android 蓝牙、低功耗BLE开发问题总结



蓝牙设备连接、断开、发送消息


连接蓝牙设备:

        BluetoothManager bluetoothManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
        BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
        BluetoothDevice device = this.bluetoothAdapter.getRemoteDevice(“蓝牙Mac地址”);
        BluetoothGatt bluetoothGatt = device.connectGatt(this, false, new BluetoothGattCallback(){

                        @Override
                        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
                            super.onConnectionStateChange(gatt, status, newState);

                            if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                                gatt.close();
                                Log.e(TAG,"连接断开" );


                            } else if(newState == BluetoothProfile.STATE_CONNECTED){
                                Log.e(TAG,"连接成功" );
                                //发现服务
                                gatt.discoverServices();
                            }
                        }

                        @Override
                        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
                            super.onServicesDiscovered(gatt, status);

                            //根据UUID获取对应服务 与读写的Characteristic

                            BluetoothGattServer service = gatt.getService(GATT_UUID);

                            //获取读写通知特征
                            BluetoothGattCharacteristic mNotifyCharacteristic = ((BluetoothGattService) service).getCharacteristic(GATT_NOTIFY_UUID);
                            BluetoothGattCharacteristic mWriteCharacteristic = ((BluetoothGattService) service).getCharacteristic(GATT_WRITE_UUID);
                            BluetoothGattCharacteristic mReadCharacteristic = ((BluetoothGattService) service).getCharacteristic(GATT_READ_UUID);
                        }
                        
						@Override
					    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
					        super.onCharacteristicChanged(gatt, characteristic);
					        //接收到蓝牙设备消息
					        Log.d(tag,"接收数据:"+ Arrays.toString(characteristic.getValue()));
					    }
                    });

断开连接

        if (bluetoothGatt != null) {
            bluetoothGatt.disconnect();
            bluetoothGatt = null;
        }

给蓝牙设备发送消息

//设置要发送的数据
mWriteCharacteristic.setValue(byte[] value)
//发送数据并接受发送状态
boolean sendState = bluetoothGatt.writeCharacteristic(mWriteCharacteristic)

蓝牙搜索

BluetoothLeScanner bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
//开始扫描
bluetoothLeScanner.startScan(ScanCallback());
//结束扫描
bluetoothLeScanner.stopScan(ScanCallback());

OTA升级

传统的ota升级,可以参考使用这个:Android-DFU-Library


如果某些定制的ota升级包可能需要厂家提供升级代码了



数据转换

ByteArray拷贝到另一个ByteArray

//  Object src : 原数组
//  int srcPos : 从元数据的起始位置开始
//  Object dest : 目标数组
//  int destPos : 目标数组的开始起始位置
//  int length  : 要copy的数组的长度
System.arraycopy(src , srcPos , dest , destPos , length  )

十六进制ByteArray转String
//Byte 为十六进制数据
String data = String.format("%02X", Byte)

String data = Byte.toChar().toString()


十六进制ByteArray转int(kotlin写法)
        var ret = 0
        if (b == null) {
            return ret
        }
        val length = b.size
        if (length > 4 || length <= 0) {
            return ret
        }
        for (i in 0 until length) {
            ret = ret shl 8
            ret += b[i].toInt() and 0xFF
        }

十六进制Byte转Int

Int data = byte & 0xFF

Int转十六进制Byte

byte data =(byte) ( int & 0xFF )

十六进制String转Int

int time = Integer.valueOf(Str, 16)

Int转ByteArray

        var iMax = 1
        val max = 4
        for (i in 0 until max) {
            val offset = 8 * (max - i - 1)
            if (int shr offset and 0xFF > 0) {
                iMax = max - i
                break
            }
        }

        val ret = ByteArray(iMax)
        for (i in 0 until iMax) {
            val offset = 8 * (iMax - i - 1)
            ret[i] = (int  shr offset and 0xFF).toByte()
        }

        return ret

其他问题

q:蓝牙扫描不到或连接不上设备

a:请检查定位权限和蓝牙权限是否开启,还有状态栏上的位置信息/GPS开关是否打开

你可能感兴趣的:(Android)