Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android筆記-藍牙開發

android筆記-藍牙開發

編輯:關於Android編程

需要的權限

第一步:獲取BluetoothAdapter,同時打開藍牙。

private void blueinit() {
        BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
        //通過藍牙管理實例獲取適配器,然後通過掃描方法(scan)獲取設備(device)
        bluetoothAdapter = bluetoothManager.getAdapter();
        //打開藍牙
        if(!bluetoothAdapter.isEnabled())
            bluetoothAdapter.enable();
    }

第二步:藍牙設備掃描

 private void saomiaohuidiao() {
        //掃描設備
        callback = new BluetoothAdapter.LeScanCallback() {
            @Override
            public void onLeScan(final BluetoothDevice bluetoothDevice, int i, byte[] bytes) {
                //重復過濾方法,列表中包含不該設備才加入列表中,並刷新列表
                if (!deviceList.contains(bluetoothDevice)&&bluetoothDevice.getName().length()>1) {
                    //將設備加入列表數據中
                    deviceList.add(bluetoothDevice);
                    handler.sendMessage(new Message());
                }

            }

        };
    }

第三步:連接通信

private void lianjietongxinhuidiao() {
        bcallback = new BluetoothGattCallback() {
            //連接狀態回調
            @Override
            public void onConnectionStateChange(final BluetoothGatt gatt, int status, final int newState) {
                super.onConnectionStateChange(gatt, status, newState);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        switch (newState) {
                            //已經連接
                            case BluetoothGatt.STATE_CONNECTED:
                                Log.d("TAG", "已連接");
                                    bluetoothGatt.discoverServices(); //獲取服務
                                break;
                            //正在連接
                            case BluetoothGatt.STATE_CONNECTING:
                                Log.d("TAG", "正在連接");
                                break;
                            //連接斷開
                            case BluetoothGatt.STATE_DISCONNECTED:
                                Log.d("TAG", "已斷開");
                                break;
                            //正在斷開
                            case BluetoothGatt.STATE_DISCONNECTING:
                                Log.d("TAG", "斷開中");
                                break;
                        }
                    }
                });

            }

            //連接上藍牙設備後觸發
            @Override
            public void onServicesDiscovered(BluetoothGatt gatt, int status) {
                super.onServicesDiscovered(gatt, status);
      //這裡可以獲得可以通信的BluetoothGattCharacteristic
            }

            //讀取
            //BluetoothGatt.writeCharacteristic(BluetoothGattCharacteristic);
            @Override
            public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
                super.onCharacteristicRead(gatt, characteristic, status);
                Log.d("讀出", characteristic.getUuid() + "-" + characteristic.getValue());
                Log.d("bcallback", "onCharacteristicRead");
            }


            //寫入觸發此方法需要調用
            //BluetoothGatt.writeCharacteristic(BluetoothGattCharacteristic);
            @Override
            public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
                super.onCharacteristicWrite(gatt, characteristic, status);
                Log.d("onCharacteristicWrite", String.valueOf(characteristic.getValue()));
            }

            @Override
            public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
                super.onCharacteristicChanged(gatt, characteristic);
                Log.d("bcallback", "onCharacteristicChanged");
            }

            @Override
            public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
                super.onDescriptorRead(gatt, descriptor, status);
                Log.d("bcallback", "onDescriptorRead");
            }

            @Override
            public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
                super.onDescriptorWrite(gatt, descriptor, status);
                Log.d("bcallback", "onDescriptorWrite");
            }

            @Override
            public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
                super.onReliableWriteCompleted(gatt, status);
                Log.d("bcallback", "onReliableWriteCompleted");
            }

            @Override
            public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
                super.onReadRemoteRssi(gatt, rssi, status);
                Log.d("bcallback", "onReadRemoteRssi");
            }

            @Override
            public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
                super.onMtuChanged(gatt, mtu, status);
                Log.d("bcallback", "onMtuChanged");
            }
        };
    }
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved