Android 蓝牙4.0在实际开发中的运用
1.藍牙搜索.
?首先是獲取BluetoothAdapter對象:
? ?final BluetoothManager bluetoothManager =
??????????????? (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
????BluetoothAdapter?? bluetoothAdapter = bluetoothManager.getAdapter();
?? 當bluetoothAdapter不為空時代表設備支持BLE4.0.
?? 然后調用BluetoothAdapter 的startLeScan(callback)方法,這里需要傳入一個callBack對象,用于搜到設備時回調。
實現如下:
?? ?private BluetoothAdapter.LeScanCallback callback =
??????????? new BluetoothAdapter.LeScanCallback() {
??????? @Override
??????? public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
??????????? runOnUiThread(new Runnable() {
??????????????? @Override
??????????????? public void run() {
????????????????? //如果搜到設備,將會執行這里,device即是搜到的設備。一般這里需要將搜到的設備添加到listView的適配器中,用于顯示到界面
??????????????? }
??????????? });
??????? }
??? };
停止搜索調用BluetoothAdapter的stopLeScan(callback)方法。
2.鏈接藍牙以及接收數據.
?連接藍牙代碼如下:
?BluetoothGatt bluetoothGatt = device.connectGatt(this, false, mGattCallback);
第一個參數是Context對象,第二個參數表示是否自動連接,第三個是一個CallBack對象,當設備連接狀態發生改變,數據的收發狀態改變時回調。
? private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
??????? @Override
??????? public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
??????????? String intentAction;
??????????? if (newState == BluetoothProfile.STATE_CONNECTED) {//連接成功
??????????? } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {//斷開連接
??????????????
??????????? }
??????? }
??????? @Override
??????? public void onServicesDiscovered(BluetoothGatt gatt, int status) {
???????
??????? }
??????? @Override
??????? public void onCharacteristicRead(BluetoothGatt gatt,
???????????????????????????????????????? BluetoothGattCharacteristic characteristic,
???????????????????????????????????????? int status) {
??????????
??????? }
??????? @Override
??????? public void onCharacteristicChanged(BluetoothGatt gatt,
??????????????????????????????????????????? BluetoothGattCharacteristic characteristic) {
? ? ? ? byte[] data = characteristic.getValue();//如果連接設備發送數據到手機端,將通過這個函數獲取數據。
??????????
??????? }
??? };
device是第一步搜到的設備BluetoothDevice對象,也可以通過獲取該設備的MAC地址,然后再獲取BluetoothDevice對象:
??????? final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(deviceMAC);
如果bluetoothGatt不為空,則連接成功.
3.藍牙發數據.
? 通過BLE發送數據時,需要知道連接設備的一些服務UUID,一般UUID都類似。
public static final UUID SERVICE = UUID
?? ??? ??? ??? ?.fromString("0000FFF0-0000-1000-8000-00805F9B34FB");
public static final UUID SEND_UUID = UUID
?? ??? ??? ??? ?.fromString("0000FFF6-0000-1000-8000-00805F9B34FB");
public static final UUID RECEIVE_UUID = UUID
?? ??? ??? ??? ?.fromString("0000FFF7-0000-1000-8000-00805F9B34FB");
public static final UUID CCC = UUID
?? ??? ??? ?.fromString("00002902-0000-1000-8000-00805f9b34fb");
有了這些UUID后就可以開始發送數據:
BluetoothGattService bluetoothGattService = bluetoothGatt
?? ??? ??? ??? ?.getService(SERVICE);?????????? //獲取UUID對應的服務。
BluetoothGattCharacteristic charac = bluetoothGattService
?? ??? ??? ??? ?.getCharacteristic(SEND_UUID); //獲取指定服務下的特性
然后就是注冊一個數據接收通知
if (bluetoothGatt.setCharacteristicNotification(charac, true)) {
?? ??? ??? ?BluetoothGattDescriptor clientConfig = charac
?? ??? ??? ??? ??? ?.getDescriptor(CCC);
?? ??? ??? ?if (clientConfig != null) {
?? ??? ??? ??? ?clientConfig
?? ??? ??? ??? ??? ??? ?.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
?? ??? ??? ??? ?bluetoothGatt.writeDescriptor(clientConfig);
?? ??? ??? ?}
?? ??? ?}
??? charac.setValue(data); //這里將要發送的數據加入該特性中,data 類型為byte數組,如果數據超過20字節需要在每次發送加入間隔。
?? ??? ?try {
?? ??? ??? ?Thread.sleep(50);
?? ??? ?} catch (InterruptedException e) {
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ??? ?bluetoothGatt.writeCharacteristic(charac);//發送數據
總結
以上是生活随笔為你收集整理的Android 蓝牙4.0在实际开发中的运用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: es like模糊匹配_Elastic
- 下一篇: 【ProCAST】铸件定向凝固仿真流程学