Android外围设备BluetoothGattServerCallback onServiceAdded()没有被调用 [英] Android Peripheral BluetoothGattServerCallback onServiceAdded() not getting called

查看:408
本文介绍了Android外围设备BluetoothGattServerCallback onServiceAdded()没有被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个简单的外围应用程序,可以在运行于Samsung S8手机上的Android Studio中进行编码.我可以很好地设置所有BLE广告和特性,但是在添加服务时.从另一个中央设备(即另一个具有用于发现蓝牙广告的应用程序的手机)连接时,我没有看到预期的结果.

So I have a simple Peripheral application that I am coding in Android Studio running on my Samsung S8 mobile phone. I can setup all my BLE Advertisements and Characteristics just fine but when I add my services. I am not seeing expected results when connected from a another Central device (ie. another mobile phone that has an app to discover bluetooth advertisements).

Android Studio的LogCat输出:

LogCat output from Android Studio:

根据日志,我只能看到2个默认服务,它们是Generic Attribute(1800)和Generic Access(1801),它们是任何Gatt服务器的默认服务.我觉得我缺少"onServiceAdded()"回调,该回调应该告诉服务是否确实已添加且可发现.

As per the Log, I can only see 2 default services that are Generic Attribute(1800) and Generic Access (1801) which are the default services for any Gatt server. I feel that I am missing the "onServiceAdded()" callback that is supposed to tell whether the service was indeed added and discoverable.

我的代码没有错误,并且addService()调用成功.我的问题是为什么我不能从onServiceAdded()返回响应?而其他诸如OverConnectionStateChange()之类的Override函数似乎可以正常工作.这似乎是非常基础的,我看了无数示例,但似乎无法使这最后一点起作用.任何有解决方案或好的参考文献的人,将不胜感激.

My code has no errors and the addService() call is successful. My question is why am I not able to get a response back from onServiceAdded()? While other Override functions like onConnectionStateChange() seem to work fine. This seems to be very basic and I looked at tons of examples but cant seem to get this last bit to work. Anyone with a solution or some good references to look at would be greatly appreciated.

推荐答案

我遇到了同样的问题(诺基亚6.1),尽管我添加了2个服务并且随机获取,但onServiceAdded仅被调用了一次

I hade the same issue (Nokia 6.1) where onServiceAdded was called only once although I was adding 2 services and I was getting randomly

onCharacteristicReadRequest() no char for handle nnn

当尝试从另一台设备连接时.

when trying to connect from another device.

我在文档

BluetoothGattServerCallback.onServiceAdded(int, BluetoothGattService)回调将指示此服务是否具有 已成功添加.在此之前不要添加其他服务 回调.

The BluetoothGattServerCallback.onServiceAdded(int, BluetoothGattService) callback will indicate whether this service has been added successfully. Do not add another service before this callback.

我找到的可靠解决方案是添加一个服务,等待调用onServiceAdded,然后添加第二个服务,再次等待onServiceAdded,然后调用startAdvertising.

The reliable solution I found was to add one service, wait for onServiceAdded to be called, then add the second service, wait for onServiceAdded again and then I call startAdvertising.

这篇关于Android外围设备BluetoothGattServerCallback onServiceAdded()没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆