智能设备BLE
蓝牙设备
目前蓝牙功能应用更多是在智能设备方面使用,用于连接智能设备与硬件,并且能够实现设备与硬件之间的通信,向硬件发送数据并接收硬件向外传递数据。达到获取硬件状态或者控制硬件运行的需求。
蓝牙搜索
获取蓝牙Adapter,用于蓝牙的搜索。使用adapter对象对蓝牙进行搜索,将搜索到的蓝牙设备添加到list中,使用ListView来进行显示。
/*获取蓝牙Adapter对象*/
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(bluetoothAdapter.isEnable()){ //判断蓝牙是否开启
bluetoothAdapter.startLeScan(mLeScanCallback);
}
/*蓝牙搜索回调*/
BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback{
runOnUiThread(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
//排查搜索到的设备,避免重复添加到显示list中
boolean flag = false;
for (BluetoothDevice device1 : list) {
if (device1.getAddress().equals(device.getAddress())) {
flag = true;
}
}
if (!flag) {
list.add(device);
if (lv_bluetooth.getAdapter() != null) {
adapter.notifyDataSetChanged();
}
}
}
});
}
});
}
设置蓝牙设备连接
通过蓝牙的Address来将设备与硬件进行连接,搭建设备与硬件之间的通信,完成与硬件的蓝牙配对。当连接完成后,其他设备将暂时无法搜索到该硬件,直到设备与硬件之间的蓝牙断开。设备与硬件连接后建立数据传输连接,实现设备与硬件之间的蓝牙通讯。
第二次编辑内容
选择将整个BLE通讯的内容用Service完成,将数据的交互放在后台中。因此,该demo涉及到了AIDL的部分,这一部分并没有贴出,之后可能会在新的文章中详细介绍。
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
if (device == null) {
return false;
}
mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
mBluetoothDeviceAddress = address;
mConnectionState = STATE_CONNECTING;
"Service内容"
/*定义在Service中,通过Service获取蓝牙状态回调*/
private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
/*连接状态改变*/
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED) {
intentAction = ACTION_GATT_CONNECTED;
mConnectionState = STATE_CONNECTED;
broadcastUpdate(intentAction);
mBluetoothGatt.discoverServices());
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
intentAction = ACTION_GATT_DISCONNECT;
mConnectionState = STATE_DISCONNECTED;
broadcastUpdate(intentAction);
}
}
@Override
/*服务发现*/
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
} else {
}
}
@Override
/*Characteristic读取*/
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
}
@Override
/*Characteristic改变*/
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
};
/*发送广播*/
private void broadcastUpdate(final String action) {
Intent intent = new Intent(action);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
/*发送广播*/
private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) {
Intent intent = new Intent(action);
if (TX_CHAR_UUID.equals(characteristic.getUuid())) {
intent.putExtra(EXTRA_DATA, characteristic.getValue());
}
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
蓝牙设备数据获取
当完成连接以后,能够在设备与硬件之间进行蓝牙数据传输。通过数据传输设备就能够获取到一定格式的硬件数据,同时也能够向硬件发送固定的指令控制硬件进行某些动作。
"BroadCastReceiver内容,用于接收CallBack中发出的广播"
private BroadcastReceiver UARTStatusChangeReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
/*GATT(数据传输)服务被发现*/
if (action.equals(UartService.ACTION_GATT_SERVICES_DISCOVERED)) {
enableTXNotification();
}
/*设备获取到数据*/
if (action.equals(UartService.ACTION_DATA_AVAILABLE)) {
//提取数据
byte[] txValue = intent.getByteArrayExtra(UartService.EXTRA_DATA);
/*连接断开*/
if (action.equals(UartService.DEVICE_DOES_NOT_SUPPORT_UART)) {
}
}
};
public void enableTXNotification() {
BluetoothGattService RxService = mBluetoothGatt.getService("6e400001-b5a3-f393-e0a9-e50e24dcca9e");
if (RxService == null) {
broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
return;
}
BluetoothGattCharacteristic TxChar = RxService.getCharacteristic("6e400003-b5a3-f393-e0a9-e50e24dcca9e");
if (TxChar == null) {
broadcastUpdate(DEVICE_DOES_NOT_SUPPORT_UART);
return;
}
mBluetoothGatt.setCharacteristicNotification(TxChar, true);
BluetoothGattDescriptor descriptor = TxChar.getDescriptor("00002902-0000-1000-8000-00805f9b34fb");
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
}