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


连接蓝牙设备:

        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开关是否打开

更多相关文章

  1. Android动态刷新listview中的数据
  2. android分页查询垃圾短信数据库信息
  3. Android(安卓)ExpandableListActivity 学习笔记
  4. android 读取网络在传输数据时的状态
  5. android 滑动加载数据
  6. Android(安卓)通过AudioRecord实时录音并转AAC
  7. android sqlite应用
  8. mybatisplus的坑 insert标签insert into select无参数问题的解决
  9. 箭头函数的基础使用

随机推荐

  1. android,内存优化详解
  2. android效率为什么这么的高呢
  3. android进程间服务通信示例
  4. Unity3D研究院之与Android相互传递消息(十
  5. Android(安卓)内存优化
  6. Android(安卓)VideoView如何播放RTSP的流
  7. Android利用硬解硬编和OpenGLES来高效的
  8. Android中Styles、Themes、attrs介绍
  9. 轻松搞定 android MVP 架构、okHttp 网络
  10. Android——填坑android studio2.3.3升级