多设备通信,使能notify蓝牙低功耗设备

星期三, 05. 九月 2018 02:03上午 – beautifulzzzz

率先需求在清单配置内部添加七个权力:

最要紧多个类:

方今无数做蓝牙( Bluetooth® )APP的都亟需同时连接多个设施,那该怎么才能而且管理多少个设备呢?以下是笔者的法门,首如若因而ArrayMap来管理不一样装备的BluetoothGatt,然后使用分别的BluetoothGatt来和从机实行数据交互,大家得以绑定该service实行形式的调用。

图片 1

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

BluetoothAdapter bluetoothAdapter

public class BluetoothLeService2 extends Service {

private ArrayMap<String, BluetoothGatt> gattArrayMap = new ArrayMap<>();
private BluetoothAdapter mBluetoothAdapter;

@Override
public void onCreate() {
    super.onCreate();
    initBluetooth();
}

/**
 * 蓝牙是否开启
 */
public boolean isBluetoothEnabled() {
    return mBluetoothAdapter.isEnabled();
}


private void initBluetooth() {
    BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return super.onStartCommand(intent, Service.START_FLAG_RETRY, startId);
}


@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}


public class LocalBinder extends Binder {
    public BluetoothLeService2 getService() {
        return BluetoothLeService2.this;
    }
}

private final IBinder mBinder = new LocalBinder();

@Override
public boolean onUnbind(Intent intent) {
    return super.onUnbind(intent);
}


/**
 * 连接设备
 *
 * @param address 设备地址
 */
public synchronized void connect(final String address) {
    BluetoothGatt gatt = gattArrayMap.get(address);
    if (gatt != null) {
        gatt.disconnect();
        gatt.close();
        gattArrayMap.remove(address);
    }
    BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
    if (device == null) return;
    device.connectGatt(this, false, mGattCallback);
}

//蓝牙连接,数据通信的回掉
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(final BluetoothGatt gatt, int status, int newState) {
        String address = gatt.getDevice().getAddress();
        if (status == BluetoothGatt.GATT_SUCCESS) {
            if (newState == BluetoothGatt.STATE_CONNECTED) {// 连接成功
                gatt.discoverServices();// 寻找服务
                gattArrayMap.put(address, gatt);
                Log.i("yushu", "connect succeed: ");
            } else if (newState == BluetoothGatt.STATE_DISCONNECTED) {// 断开连接
                onDisConnected(gatt, address);
                Log.i("yushu", "connect fail ");
            }
        } else {
            onDisConnected(gatt, address);
            Log.i("yushu", "connect fail ");
        }
    }


    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            enableNotification(gatt);//notification
        }
    }


    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
        byte[] value = characteristic.getValue();
/**
 * 这里可以拿到设备notification回来的数据
*/

    }


    @Override
    public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
        super.onReadRemoteRssi(gatt, rssi, status);
        if (status == BluetoothGatt.GATT_SUCCESS) {

        }
    }
};


private void onDisConnected(BluetoothGatt gatt, String address) {
    gatt.disconnect();
    gatt.close();
}


/**
 * 使能通知
 */
private void enableNotification(BluetoothGatt mBluetoothGatt) {
    if (mBluetoothGatt == null) return;
    BluetoothGattService ableService = mBluetoothGatt.getService(UUIDUtils.UUID_LOST_SERVICE);
    if (ableService == null) return;
    BluetoothGattCharacteristic TxPowerLevel = ableService.getCharacteristic(UUIDUtils.UUID_LOST_ENABLE);
    if (TxPowerLevel == null) return;
    setCharacteristicNotification(mBluetoothGatt, TxPowerLevel, true);
}

/**
 * 使能通知
 */
private void setCharacteristicNotification(BluetoothGatt mBluetoothGatt, BluetoothGattCharacteristic characteristic, boolean enabled) {
    if (mBluetoothGatt == null) return;
    mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
    if (UUIDUtils.UUID_LOST_ENABLE.equals(characteristic.getUuid())) {
        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUIDUtils.CLIENT_CHARACTERISTIC_CONFIG);
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        mBluetoothGatt.writeDescriptor(descriptor);
    }
}


/**
 * 写数据到硬件,这里的服务UUID和特这的UUID参考硬件那边,两边要对应
 */
public synchronized void writeDataToDevice(byte[] bs, String address) {
    BluetoothGatt mBluetoothGatt = gattArrayMap.get(address);
    if (mBluetoothGatt == null) return;
    BluetoothGattService alertService = mBluetoothGatt.getService(UUIDUtils.UUID_LOST_SERVICE);
    if (alertService == null) return;
    BluetoothGattCharacteristic alertLevel = alertService.getCharacteristic(UUIDUtils.UUID_LOST_WRITE);
    if (alertLevel == null) return;
    alertLevel.setValue(bs);
    alertLevel.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
    mBluetoothGatt.writeCharacteristic(alertLevel);
}


public boolean readRssi(String address) {
    BluetoothGatt bluetoothGatt = gattArrayMap.get(address);
    return bluetoothGatt != null && bluetoothGatt.readRemoteRssi();
}


public void remove(String address) {
    BluetoothGatt bluetoothGatt = gattArrayMap.get(address);
    if (bluetoothGatt != null) {
        bluetoothGatt.disconnect();
        bluetoothGatt.close();
        gattArrayMap.remove(address);
    }
}

public void disConnect(String address) {
    BluetoothGatt bluetoothGatt = gattArrayMap.get(address);
    if (bluetoothGatt != null) {
        bluetoothGatt.disconnect();
        gattArrayMap.remove(address);
    }
}


@Override
public void onDestroy() {
    gattArrayMap.clear();
    super.onDestroy();
}
}

1、前言

上一篇讲了何等编写翻译安装BlueZ-5,本篇首要在于玩BlueZ,用命令行去操作BLE设备:

  • [BlueZ] 1、Download install and use the BlueZ and hcitool on PI
    3B+

图片 2

android里面蓝牙( Bluetooth® )是透过蓝牙5.0艾达pter来拓展操作的,所以率先大家须求得到到BluetoothAdapter的实例

BluetoothSocket btSocket;

二 、gatttool —— 老工具趟坑

刚伊始接着 Get Started with Bluetooth Low Energy on
Linux
操作gatttool,发现坑太多(主因是工具老了):

采用sudo gatttool -b 4D:69:98:0E:91:5E -I去连接
察觉会报错:Error: connect error: Connection refused (111)
终极参考LINK-11发现须求加random选项([\#1](https://stackoverflow.com/questions/32947807/cannot-connect-to-ble-device-on-raspberry-pi))

➜  ~  sudo gatttool -b 4D:69:98:0E:91:5E -I
[4D:69:98:0E:91:5E][LE]> connect
Attempting to connect to 4D:69:98:0E:91:5E
Error: connect error: Connection refused (111)
[4D:69:98:0E:91:5E][LE]> exit
➜  ~  sudo gatttool  -t random  -b 4D:69:98:0E:91:5E -I
[4D:69:98:0E:91:5E][LE]> connect
Attempting to connect to 4D:69:98:0E:91:5E
Connection successful
[4D:69:98:0E:91:5E][LE]> 
(gatttool:3104): GLib-WARNING **: Invalid file descriptor.

过贰次会10S机动断开,网上说那几个工具老了,不建议用了([\#2](https://www.spinics.net/lists/linux-bluetooth/msg67617.html)):多设备通信,使能notify蓝牙低功耗设备。

There are new tools to use with GATT, bluetoothctl/bluetoothd is the preferred since with that you have GAP, etc, 
but if want to use a stand alone tool then I suggest you use btgatt-client.

图片 3

//先获取BlueToothAdapter的实例
BluetoothAdapter blueToothAdapter = BluetoothAdapter.getDefaultAdapter();

BluetoothDevice device

三 、bluetoothctl——NB的新工具

命令行进入bluetoothctl操作环境([\#6](https://mcuoneclipse.com/2016/12/19/tutorial-ble-pairing-the-raspberry-pi-3-model-b-with-hexiwear/))

bluetoothctl

自身在手提式有线话机上用lightblue模拟3个BLE设备ty_prod,之后对其service实行修改,调用scan
on举办搜索照旧老的,
末尾发现要先用remove移除从前的设施,之后再scan就会现出[NEW] Device 72:3B:E1:81:4E:4F ty_prod设备
注: 用lightblue模拟的装置的MAC不是定位的
注:
笔者发觉在lightblue中不管怎么模拟BLE设备,一旦被连上搜索到的service都是IPone的

[bluetooth]# devices
Device 28:ED:6A:A0:26:B7 ty_prod
Device 58:71:33:00:00:24 Bluetooth Keyboard
Device 00:1A:7D:DA:71:0A SHEN-PC
Device 94:87:E0:B3:AC:6F Mi Phone
[bluetooth]# remove 28:ED:6A:A0:26:B7 
...
[bluetooth]# scan on
Discovery started
[NEW] Device 72:3B:E1:81:4E:4F ty_prod
[bluetooth]# scan off
...
Discovery stopped
[bluetooth]# connect 72:3B:E1:81:4E:4F
Attempting to connect to 72:3B:E1:81:4E:4F
[CHG] Device 72:3B:E1:81:4E:4F Connected: yes
Connection successful
[ty_prod]

干脆就用三星手提式无线电话机自带的服务做测试了~

[ty_prod]# info
Device 28:ED:6A:A0:26:B7 (public)
    Name: tuya_mdev_test
    Alias: tuya_mdev_test
    Appearance: 0x0040
    Icon: phone
    Paired: yes
    Trusted: no
    Blocked: no
    Connected: yes
    LegacyPairing: no
    UUID: Fax                       (00001111-0000-1000-8000-00805f9b34fb)
    UUID: Generic Access Profile    (00001800-0000-1000-8000-00805f9b34fb)
    UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)
    UUID: Current Time Service      (00001805-0000-1000-8000-00805f9b34fb)
    UUID: Device Information        (0000180a-0000-1000-8000-00805f9b34fb)
    UUID: Battery Service           (0000180f-0000-1000-8000-00805f9b34fb)
    UUID: Vendor specific           (7905f431-b5ce-4e99-a40f-4b1e122d00d0)
    UUID: Vendor specific           (89d3502b-0f36-433a-8ef4-c502ad55f8dc)
    UUID: Vendor specific           (9fa480e0-4967-4542-9390-d343dc5d04ae)
    UUID: Vendor specific           (d0611e78-bbb4-4591-a5f8-487910ae4366)
[CHG] Device 28:ED:6A:A0:26:B7 ServicesResolved: no
[CHG] Device 28:ED:6A:A0:26:B7 Connected: no

咱俩用Current 提姆e Service,列出全部attributes操作如下:

[tuya_mdev_test]# menu gatt
[tuya_mdev_test]# list-attributes 28:ED:6A:A0:26:B7
...
Primary Service
    /org/bluez/hci0/dev_47_B1_26_C1_81_18/service0041
    00001805-0000-1000-8000-00805f9b34fb
    Current Time Service
Characteristic
    /org/bluez/hci0/dev_47_B1_26_C1_81_18/service0041/char0045
    00002a0f-0000-1000-8000-00805f9b34fb
    Local Time Information
Characteristic
    /org/bluez/hci0/dev_47_B1_26_C1_81_18/service0041/char0042
    00002a2b-0000-1000-8000-00805f9b34fb
    Current Time
Descriptor
    /org/bluez/hci0/dev_47_B1_26_C1_81_18/service0041/char0042/desc0044
    00002902-0000-1000-8000-00805f9b34fb
    Client Characteristic Configuration
...

上面Current Time Service相应的劳动如下图:

图片 4

咱们选取Current Time进行操作UUID:0x2A2B

[ty_prod]# select-attribute /org/bluez/hci0/dev_47_B1_26_C1_81_18/service0041/char0042
[tuya_mdev_test:/service0041/char0042]# read
Attempting to read /org/bluez/hci0/dev_47_B1_26_C1_81_18/service0041/char0042
[CHG] Attribute /org/bluez/hci0/dev_47_B1_26_C1_81_18/service0041/char0042 Value:
  e2 07 09 05 01 24 11 03 f1 02                    .....$....      
  e2 07 09 05 01 24 11 03 f1 02                    .....$.... 
[tuya_mdev_test:/service0041/char0042]# attribute-info
Characteristic - Current Time
    UUID: 00002a2b-0000-1000-8000-00805f9b34fb
    Service: /org/bluez/hci0/dev_47_B1_26_C1_81_18/service0041
    Value:
  e2 07 09 05 01 2e 01 03 f5 02                    ..........      
    Notifying: yes
    Flags: read
    Flags: notify

读出结果大约意思应该是:2018-9/5-1:36:17 周三

读取一下0x180A的Device Information:

[tuya_mdev_test:/service0006/char0007]# select-attribute /org/bluez/hci0/dev_47_B1_26_C1_81_18/service0047/char004a
[tuya_mdev_test:/service0047/char004a]# attribute-info
Characteristic - Model Number String
    UUID: 00002a24-0000-1000-8000-00805f9b34fb
    Service: /org/bluez/hci0/dev_47_B1_26_C1_81_18/service0047
    Flags: read
[tuya_mdev_test:/service0047/char004a]# read
Attempting to read /org/bluez/hci0/dev_47_B1_26_C1_81_18/service0047/char004a
[CHG] Attribute /org/bluez/hci0/dev_47_B1_26_C1_81_18/service0047/char004a Value:
  69 50 68 6f 6e 65 36 2c 32                       iPhone6,2       
  69 50 68 6f 6e 65 36 2c 32                       iPhone6,2    

理所当然写、使能notify也很简短,看help即可。最终断开连接、并退出!!!

[tuya_mdev_test:/service0047/char004a]# disconnect 28:ED:6A:A0:26:B7
Attempting to disconnect from 28:ED:6A:A0:26:B7
[CHG] Device 28:ED:6A:A0:26:B7 ServicesResolved: no
Successful disconnected
[CHG] Device 28:ED:6A:A0:26:B7 Connected: no
[bluetooth]# quit

图片 5

在寻觅从前,我们得以先拿走与大家配对过的配备

bluetoothAdapter =
BluetoothAdapter.getDefaultAdapter();

LINKS

[1].Cannot connect to BLE device on Raspberry
Pi
[2].Invalid file descriptor gatttool of bluez
5.32
[3].Get Started with Bluetooth Low Energy on
Linux
[4].Reverse Engineering a Bluetooth Low Energy Light
Bulb
[5].Doing Bluetooth Low Energy on
Linux
[6].Tutorial: BLE Pairing the Raspberry Pi 3 Model B with
Hexiwear

图片 6

@beautifulzzzz
智能硬件、物联网,热爱技术,关注产品
博客:http://blog.beautifulzzzz.com
园友交流群:414948975
//获取已经配对过的设备的集合
Set<BluetoothDevice> bondedDevices = blueToothAdapter.getBondedDevices()

bluetoothAdapter.startDiscovery();//伊始搜索附近的蓝牙5.0设备,搜索的时候会发生三个广播如下

可是想要获取到配对过的设施,咱们亟须是在

startDiscovery()方法是贰个异步方法,它会对任何蓝牙5.0设备开始展览搜寻,持续时间为12秒。搜索进度实际上是在System
Service中开始展览,大家能够由此cancelDiscovery()方法来终止那一个搜索。在系统查找Bluetooth设备的历程中,系统只怕会发送以下多少个广播:ACTION_DISCOVERY_STA本田CR-VT(开头搜寻),ACTION_DISCOVERY_FINISHED(搜索甘休)和ACTION_FOUND(找到设备)。ACTION_FOUND这些才是我们想要的。所以在startDiscovery以前大家应该登记八个广播如下:

//手机有蓝牙设备并且蓝牙是打开的
blueToothAdapter != null && blueToothAdapter.isEnabled()
IntentFilter intentfilter = new IntentFilter(
                BluetoothDevice.ACTION_FOUND);
        bluetoothReceiver = new BluetoothReceiver();
        // 注册bluetoothReceiver
        registerReceiver(bluetoothReceiver, intentfilter);

下边大家谈一下Bluetooth的<b>打开药情势</b>,近年来我知道的法子有3种
第一种:

播音接收类蓝牙( Bluetooth® )( Bluetooth® )Receiver的达成如下:

//强制打开蓝牙
blueToothAdapter.enable();
   public class BluetoothReceiver extends BroadcastReceiver {

        @SuppressLint("ShowToast")
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Toast.makeText(MainActivity.this, "扫描中...", 100).show();
            // device对象代表被扫描到的远程设备的对象
            // 将搜索到的蓝牙设备在ListView中显示出来

            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                BluetoothDevice device = intent
                        .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                String str = device.getName() + "|" + device.getAddress();

                if (lstDevices.indexOf(str) == -1)// 防止重复添加
                    lstDevices.add(str); // 获取设备名称和mac地址
                adtDevices.notifyDataSetChanged();

            }

        }

    }

第二种:

private List<String> lstDevices = new ArrayList<String>();