<_安卓蓝牙 gatt 传输文件">
当前位置:   article > 正文

Android 蓝牙GATT连接发送数据简单流程_安卓蓝牙 gatt 传输文件

安卓蓝牙 gatt 传输文件

Android 蓝牙GATT连接发送数据简单流程

  1. 给权限(系统5.0以上蓝牙扫描需要定位权限还需要在代码里动态申请后面有代码)
<!-- 蓝牙权限 -->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<!-- 定位权限 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  • 1
  • 2
  • 3
  • 4
  • 5
动态权限申请
/**
     * 申请运行时权限,不授予会搜索不到设备
     */
    private void initPermissions() {
        if (ContextCompat.checkSelfPermission(this, "android.permission-group.LOCATION") != 0) {
            ActivityCompat.requestPermissions(
                    this,
                    new String[]{
                            "android.permission.ACCESS_FINE_LOCATION",
                            "android.permission.ACCESS_COARSE_LOCATION",
                            "android.permission.ACCESS_WIFI_STATE"},
                    1
            );
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  1. 获取BluetoothManager和BluetoothAdapter
final BluetoothManager bluetoothManager =
                (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
  • 1
  • 2
  • 3
  1. 我这里新建了一个蓝牙连接类 BleConnect 用来管理蓝牙连接的方法。
public BleConnect(BluetoothAdapter mBluetoothAdapter, Context mContext) {
        this.mBluetoothAdapter = mBluetoothAdapter;
        this.mContext = mContext;
    }
  • 1
  • 2
  • 3
  • 4
  1. 写一个搜索设备广播接收器
/**
     * 搜索到新设备广播广播接收器
     */
    private final BroadcastReceiver searchReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // 这就是蓝牙设备
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                
                //做出一些判断你要连接哪台蓝牙设备
                TODO();
                
                //获取到BlueToothGatt对象
                mBluetoothGatt = device.connectGatt(mContext, false, mGattCallback);
                //搜索到设备直接连接(后面有函数代码)
                connectGatt(device);
                Log.d("juerany", "get: "+device.getName()+"--"+device.getAddress());
                //停止搜索
                mBluetoothAdapter.cancelDiscovery();
            }

        }
    }
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  1. 蓝牙连接回调函数
/**
     * 连接成功或者失败的回调函数
     */
    public final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            Log.d(TAG, "onConnectionStateChange: "+newState);

            if (newState == BluetoothProfile.STATE_CONNECTED) {
                //bluetooth is connected so discover services
                Log.d(TAG, "onConnectionStateChange: "+mBluetoothGatt.getDevice().getName());
                mBluetoothGatt.discoverServices();

            } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                //Bluetooth is disconnected
                Log.d(TAG, "onConnectionStateChange: disconnected");
            }
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                // services are discoverd
                Log.d(TAG, "onServicesDiscovered: GATT_SUCCESS");
                mBluetoothGatt = gatt;
            }
            List<BluetoothGattService> list = gatt.getServices();
            for(BluetoothGattService service: list){
                for(BluetoothGattCharacteristic characteristic : service.getCharacteristics()){
                    //获取到相应的服务UUID和特征UUID
                    TODO();
                    }
                }
            }

        }

        @Override
        public void onCharacteristicRead(BluetoothGatt gatt,
                                         BluetoothGattCharacteristic characteristic,
                                         int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {

            }
        }

        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt,
                                            BluetoothGattCharacteristic characteristic) {

        }

        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicWrite(gatt, characteristic, status);

        }
    };
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  1. 向设备发送数据的函数
/**
     * 向设备发送数据
     * @param value
     * @return
     */
    public boolean writeRXCharacteristic(byte[] value) {
        BluetoothGattService RxService = mBluetoothGatt.getService("服务UUID");
        if (RxService == null) {
            //Service not supported
            Log.d(TAG, "writeRXCharacteristic: Service not supported");
            return false;
        }
        BluetoothGattCharacteristic RxChar = RxService.getCharacteristic("特征UUID");
        if (RxChar == null) {
            // service not supported
            Log.d(TAG, "writeRXCharacteristic: Service not supported");
            return false;
        }
        RxChar.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
        RxChar.setValue(value);
        Log.d(TAG, "writeRXCharacteristic: ");
        return mBluetoothGatt.writeCharacteristic(RxChar);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  1. 扫描设备
/**
     * 搜索设备
     */
    public void searchDevice(){
        if (mBluetoothAdapter.isDiscovering()) {
            mBluetoothAdapter.cancelDiscovery();
        }
        if(mBluetoothAdapter != null){
            //注册搜索广播
            IntentFilter foundFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
            mContext.registerReceiver(searchReceiver, foundFilter);
            //开始搜索
            mBluetoothAdapter.startDiscovery();
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

最后

先写这么多,之后有时间再更新 把队列加入进蓝牙发送里面,防止粘包。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/573736
推荐阅读
相关标签