GATT回调无法注册 [英] GATT callback fails to register

查看:2193
本文介绍了GATT回调无法注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着写了蓝牙低耗能,之后将在我的周围被传递由UART发送信息的应用程序。我遵循的步骤这里和应用程序扫描并成功找到该设备。但是,使用BluetoothGatt = BluetoothDevice.connectGatt(背景下,自动连接,回调)方法失败,与连接的logcat说:无法注册的回调。

I'm trying to write an application to send messages over Bluetooth Low Energy, which will then be passed on by UART in my peripheral. I've followed the steps here and the app scans for and finds the device successfully. However, connection using the BluetoothGatt = BluetoothDevice.connectGatt(context, autoconnect, callback) method fails, with logcat saying "Failed to register callback".

呼叫制成:

//device scan callback
private BluetoothAdapter.LeScanCallback btScanCallback = new BluetoothAdapter.LeScanCallback() 
{
    @Override
    public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord)
    {       
        some stuff
        currBtGatt = device.connectGatt(parentActivity, false, btGattCallback);
    }
};

和关贸总协定回调:

//GATT callback
private BluetoothGattCallback btGattCallback = new BluetoothGattCallback()
{       
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState)
    {
        // if connected successfully
        if(newState == BluetoothProfile.STATE_CONNECTED)
        {
            //discover services
            updateStatus("Connected");
            gatt.discoverServices();
        }
        else if(newState == BluetoothProfile.STATE_DISCONNECTED)
        {
            updateStatus("Disconnected");
        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status)
    {
        if(status == BluetoothGatt.GATT_SUCCESS)
        {
            //pick out the (app side) transmit channel
            currBtService = gatt.getService(uartUuids[0]);
            currBtCharacteristic = currBtService.getCharacteristic(uartUuids[1]);
        }
        else 
        {
            updateStatus("Service discovery failed");
        }
    }
};

LogCat中说:

Logcat says:

11-19 10:40:39.363: D/BluetoothAdapter(11717): stopLeScan()
11-19 10:40:39.373: D/BluetoothGatt(11717): connect() - device: DC:6D:75:0C:0F:F9, auto: false
11-19 10:40:39.373: D/BluetoothGatt(11717): registerApp()
11-19 10:40:39.373: D/BluetoothGatt(11717): registerApp() - UUID=3ba20989-5026-4715-add3-a5e31684009a
11-19 10:40:39.373: I/BluetoothGatt(11717): Client registered, waiting for callback
11-19 10:40:49.373: E/BluetoothGatt(11717): Failed to register callback
11-19 10:40:49.533: D/BluetoothGatt(11717): onClientRegistered() - status=0 clientIf=5
11-19 10:40:49.533: E/BluetoothGatt(11717): Bad connection state: 0
11-19 10:40:49.593: D/BluetoothGatt(11717): onClientConnectionState() - status=0 clientIf=5 device=DC:6D:75:0C:0F:F9
11-19 10:40:49.593: W/BluetoothGatt(11717): Unhandled exception: java.lang.NullPointerException

有趣的是,我的外围移动到已连接状态(我有指示LED),我可以与演示应用或与PC BLE加密狗连接到它从相同电话。任何想法AP preciated。

Interestingly, my peripheral moves to a "connected" state (I have indication LEDs) and I can connect to it from the same phone with a demonstration application or with a PC BLE dongle. Any ideas appreciated.

connectGatt方法返回null,这是我猜的预期。

the connectGatt method returns null, which I guess is expected.

关于API 18来源$ C ​​$ C检,看来无法登记回调的消息被传递,因为该方法registerApp()返回false,因为IBluetoothGattMSERVICE的registerClient()方法抛出一个远程异常,可能是在该行:

On inspection of API 18 source code, it appears that the "Failed to register callback" message is delivered because the method registerApp() returns false because the registerClient() method of the IBluetoothGatt "mService" throws a remote exception, probably at the line:

enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");

因为在非常下一行日志消息是从未见过。因此,它可能是一个权限的事情,除了应用程序的的蓝牙和bluetooth_admin权限。

because the log message in the very next line is never seen. So it could be a permissions thing, except that the application has bluetooth and bluetooth_admin permissions.

推荐答案

我终于想通这个问题了。我使用的设备是三星Galaxy S4和实际问题(感谢维布勒在你的答案指导,但你稍微偏离你的结论)似乎是一个线程问题。

I finally figured this problem out. The device I am using is a Samsung Galaxy S4 and the actual problem (thanks Wibble for guidance in your answer, but you are slightly off in your conclusion) appears to be a threading issue.

在维布勒的回答,他说,添加一个按钮,连接固定他的问题。我开始想,为什么是重要的,而且我也可以在整个会话期间没有图形用户界面按钮,使用后台工作线程连接和断开的罚款。当我强迫关闭我的应用程序,重新启动,并尝试连接,我开始得到错误无法注册的回调。并没有什么作品了。我几乎把我的头发在这一个:)

In Wibble's answer, he stated that adding a button to connect fixed his issue. I started wondering why that matters, and I also can connect and disconnect fine during an entire session without a GUI button using background worker threads. As soon as I force close my application, restart it, and try to connect, I start getting the error "Failed to register callback." and nothing works any more. I almost pulled my hair out over this one :)

请参阅我的<一个href="http://developer.samsung.com/forum/thread/ble-43-connection-failures/201/249986?boardName=SDK&startId=zzzzz~&searchSubId=0000000007">post在三星论坛在我确切的问题,更多的细节。

See my post in Samsung's forums for more detail on my exact issues.

解决方法: 为了解决这个问题,只要确保你运行任何BLE交互code(设备#connectGatt,连接,断开连接等)在UIThread code(与处理程序,本地服务或活动#runOnUiThread)。按照这个经验法则,你会希望避免这个可怕的问题。

Solution: To get around this issue, just make sure you run any BLE interaction code (device#connectGatt, connect, disconnect, etc) code in the UIThread (with a handler, local service, or Activity#runOnUiThread). Follow this rule of thumb and you will hopefully avoid this dreadful problem.

深在我们的图书馆,我只用了访问应用程序上下文。您可以从上下文一个处理程序,将张贴到主线程使用新的处理程序(ctx.getMainLooper());

Deep in our library, I only had access to the application context. You can create a handler from a context that will post to the main thread by using new Handler(ctx.getMainLooper());

如果您遇到其他连接问题,部署的示例应用程序样本\ Android为18 \遗留\ BluetoothLeGatt ,看看有没有应用程序的工作。那是一种我基本实现BLE我的周围没有实际工作,并给了我希望,如果我在我们的库中挖出的是我最终找到了答案。

If you face other connection problems, deploy the sample app in samples\android-18\legacy\BluetoothLeGatt and see if that application works. That was kind of my baseline for realizing BLE does actually work with my peripheral, and gave me hope that if I dug enough in our library I would eventually find the answer.

编辑:我没有看到这个无法注册的回调使用后台线程时执行BLE操作上的Nexus 4,Nexus的5或7的Nexus 2013的问题。这可能只是在三星4.3实现的问题。

I did not see this 'Failed to register callback' issue on the Nexus 4, Nexus 5, or Nexus 7 2013 when using background threads to perform BLE operations. It may just be an issue in Samsungs 4.3 implementation.

这篇关于GATT回调无法注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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