ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

了解Android低功耗蓝牙的特性

2019-11-22 06:36:56  阅读:263  来源: 互联网

标签:bluetooth bluetooth-lowenergy android


我正在尝试简单地从银河S3读取和写入Hello World到连接到虚拟串行端口的blueradio软件狗.但我越来越

Unhandled exception: java.lang.NullPointerException

每当我打电话

gatt.readCharacteristic(characteristic);

我用它来定义特征

private static final UUID MY_UUID = UUID.fromString("00001801-0000-1000-8000-00805f9b34fb");
private static final UUID charUUID = UUID.fromString("00002a01-0000-1000-8000-00805f9b34fb");
characteristic = gatt.getService(MY_UUID).getCharacteristic(charUUID);

当我这样调用discoverServices()时,我从LogCat中获取了UUID

D/BluetoothGatt(7083): discoverServices() - device: EC:FE:7E:11:12:A4
D/BluetoothGatt(7083): onGetService() - Device=EC:FE:7E:11:12:A4 UUID=00001800-0000-1000-8000-00805f9b34fb
D/BluetoothGatt(7083): onGetService() - Device=EC:FE:7E:11:12:A4 UUID=00001801-0000-1000-8000-00805f9b34fb
D/BluetoothGatt(7083): onGetService() - Device=EC:FE:7E:11:12:A4 UUID=0000180f-0000-1000-8000-00805f9b34fb
D/BluetoothGatt(7083): onGetService() - Device=EC:FE:7E:11:12:A4 UUID=da2b84f1-6279-48de-bdc0-afbea0226079
D/BluetoothGatt(7083): onGetCharacteristic() - Device=EC:FE:7E:11:12:A4 UUID=00002a00-0000-1000-8000-00805f9b34fb
D/BluetoothGatt(7083): onGetCharacteristic() - Device=EC:FE:7E:11:12:A4 UUID=00002a01-0000-1000-8000-00805f9b34fb
D/BluetoothGatt(7083): onGetCharacteristic() - Device=EC:FE:7E:11:12:A4 UUID=00002a02-0000-1000-8000-00805f9b34fb
D/BluetoothGatt(7083): onGetCharacteristic() - Device=EC:FE:7E:11:12:A4 UUID=00002a03-0000-1000-8000-00805f9b34fb

这是我不确定我是否做对的地方,我不知道如何为特性和服务获取正确的UUID

Beloiw是我的完整回调函数

private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {

    public void testFunction(BluetoothGatt gatt){
        Log.d(TAG, "In Test Function");
        gatt.readRemoteRssi();
        BluetoothGattCharacteristic characteristic;
        characteristic = gatt.getService(MY_UUID).getCharacteristic(charUUID);
        characteristic.setValue("Hello World");
        gatt.readCharacteristic(characteristic);
    }
     @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status,
            int newState) {


            gatt.discoverServices();


    }


@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    Log.d(TAG, "Services Discovered: "+ status);
    //mHandler.sendMessage(Message.obtain(null, MSG_PROGRESS, "Enabling Sensors..."));
    /*
     * With services discovered, we are going to reset our state machine and start
     * working through the sensors we need to enable
     */
            testFunction(gatt);
}


public void     onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic){
    Log.d(TAG, "Characteristic Changed: "+ characteristic.getValue());
}

public void     onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status){
    Log.d(TAG, "Characteristic Read: "+ status);
}

public void     onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status){
    Log.d(TAG, "Characteristic Write: "+ status);
}



public void     onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status){
    Log.d(TAG, "Descriptor Read: "+ status);
}

public void     onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status){
    Log.d(TAG, "Descriptor Write: "+ status);
}

public void     onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status){
    Log.d(TAG, "Read Rssi: "+ status);
}

public void     onReliableWriteCompleted(BluetoothGatt gatt, int status){
    Log.d(TAG, "Reliable Write: "+ status);
}


};

解决方法:

UUID由bluetooth.org预定义,它定义了前缀,例如00001800,最后一部分“ 0000-1000-8000-00805f9b34fb”相同.

请参阅预定义的UUID here列表.

标签:bluetooth,bluetooth-lowenergy,android
来源: https://codeday.me/bug/20191122/2057706.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有