不会调用Android BLE服务onServiceDiscover()

时间:2017-10-30 08:59:24

标签: android bluetooth-lowenergy

Ble广告在BluetoothGattServer中添加广告数据并完成广告并且设备连接。在连接之后,还调用了discoverServices(),但是没有发现服务,即没有触发onServiceDiscovered()回调。

// BluetoothGatt客户端可以正常使用Google示例Adevertiser 我已经测试但是服务器端我添加了下面列出的BluetoothGattServer代码

// ADVERTISER CODE / **      *开始BLE广告。      * /

private void startAdvertising() {
    Log.d(TAG, "Service: Starting Advertising");

    if (mAdvertiseCallback == null) {
        AdvertiseSettings settings = buildAdvertiseSettings();
        AdvertiseData data = buildAdvertiseData();
        mAdvertiseCallback = new SampleAdvertiseCallback();

        if (mBluetoothLeAdvertiser != null) {
            mBluetoothLeAdvertiser.startAdvertising(settings, data, mAdvertiseCallback);
        }
    }
}

// GATT SERVER创建gatt服务器实例附加将要公开的服务和特征

private void initServer() {
     BluetoothGattService service =
            new BluetoothGattService(Constants.Service_UUID,BluetoothGattService.SERVICE_TYPE_PRIMARY);

    BluetoothGattCharacteristic characteristic =
            new BluetoothGattCharacteristic(Constants.Char_UUID,
                    //Read + write characteristics and permission
                    BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_WRITE,
                    BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_WRITE);

    service.addCharacteristic(characteristic);
    mBluetoothGattServer.addService(service);
}

//回调处理所有Gatt客户端连接以处理读写请求

private BluetoothGattServerCallback mBGattServerCallback = new BluetoothGattServerCallback() {
    @Override
    public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
        super.onConnectionStateChange(device, status, newState);
        Log.d(TAG ,"OnConnection state change");
        if(newState == BluetoothProfile.STATE_CONNECTED){

        }else  if(newState == BluetoothProfile.STATE_DISCONNECTED){

        }

    }

    @Override
    public void onServiceAdded(int status, BluetoothGattService service) {
        super.onServiceAdded(status, service);
        Log.i(TAG, " Service are added");
    }

//广告数据

private AdvertiseData buildAdvertiseData() {

AdvertiseData data = new AdvertiseData.Builder() .setIncludeDeviceName(true) .addServiceUuid(new ParcelUuid(Constants.Service_UUID)) .build(); }

//广告设置

private AdvertiseSettings buildAdvertiseSettings() {
    AdvertiseSettings settings = new AdvertiseSettings.Builder()
            .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED)
            .setConnectable(true)
            .setTimeout(0)
            .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_MEDIUM)
            .build();
    return settings;

}

//如果我们还没有系统蓝牙对象,请获取对系统蓝牙对象的引用。

private void initialize() {
    if (mBluetoothLeAdvertiser == null) {
        BluetoothManager mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        if (mBluetoothManager != null) {
            BluetoothAdapter mBluetoothAdapter = mBluetoothManager.getAdapter();
            if (mBluetoothAdapter != null) {
                mBluetoothLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser();
                //register the instance of server to BLE framework
                mBluetoothGattServer = mBluetoothManager.openGattServer(this,mBGattServerCallback);
                initServer();
            } else {
                Toast.makeText(this, getString(R.string.bt_null), Toast.LENGTH_LONG).show();
            }
        } else {
            Toast.makeText(this, getString(R.string.bt_null), Toast.LENGTH_LONG).show();
        }
    }
}

//记录夏日

`     //服务uuid​​和特征uuid正在进入广告数据 { onGetService() - Device = 65:5E:34:E1:47:55 UUID = 0000xxxx-0000-1000-8000-00805xxxx                   10-31 17:55:11.542 23276-25749 /  D / BluetoothGatt:onGetService() - Device = 65:5E:34:E1:47:55 UUID = 0000xxxx-0000-                   1000-8000-0080xxxxx

onGetCharacteristic() - Device = 65:5E:34:E1:47:55 UUID = 0000xxxxx-0000-1000-8000-00805fxxxxx srvcType = 0 srvcInstId = 0 charInstId = 0 charProps = 32 10-31 17:55:11.627 23276-23290 / D / BluetoothGatt:onGetCharacteristic() - Device = 65:5E:34:E1:47:55 UUID = 0000xxx-0000-1000-8000-00805fxxxx srvcType = 0 srvcInstId = 0 charInstId = 1 charProps = 10 10-31 17:55:11.718 23276-23289 / D / BluetoothGatt:onGetCharacteristic() - Device = 65:5E:34:E1:47:55 UUID = 0000xxxx-0000-1000-8000-00805fxxxxx srvcType = 0 srvcInstId = 0 charInstId = 0 charProps = 2 } `

1 个答案:

答案 0 :(得分:0)

设备更改 我正在测试micromax canvas作为Central。 现在我改变了设备,这里的服务被发现并得到回调..

<强> BUT writeCharacteristics()请求,服务器(远程设备)没有触发回调onCharacteristicsReadRequest(),因为我只执行所需特征的写操作。没有别的拒绝操作。