Android智慧裝置藍芽連線(BLE)
阿新 • • 發佈:2019-02-02
智慧裝置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);
}