蓝牙连接不上4.4.2 [英] Bluetooth not connecting on 4.4.2

查看:328
本文介绍了蓝牙连接不上4.4.2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了已连接上,我曾尝试之前4.4.2所有版本的Andr​​oid的蓝牙设备。现在,它没有连接上的Galaxy Tab 4或S3。该选项卡3连接罚款4.1.2。这个问题似乎试图初始化的BluetoothSocket 出现在 AcceptThread 。在code我使用的是不基于SDK中聊天的例子。

I've got a Bluetooth device that has connected on all versions of Android that I have tried prior to 4.4.2. Now, it's not connecting on the Galaxy Tab 4 or the S3. The Tab 3 connects fine with 4.1.2. The problem seems to occur in the AcceptThread while trying to initialize the BluetoothSocket. The code I'm using is based off of the chat example in the sdk.

我接受code

private class AcceptThread extends Thread {
    // The local server socket
    private BluetoothServerSocket mmServerSocket;
    public boolean successInit = false;

    public AcceptThread() {
        closeAllConnections();          
        BluetoothServerSocket tmp = null;

        // Create a new listening server socket
        while(!successInit) {
            try {
                tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
                successInit= true;
                Log.i("TAG", "Socket Server Created");
            }
            catch(Exception e) {
                Log.i("TAG", e.getMessage());
                successInit = false;
            }
        }

        mmServerSocket = tmp;
    }
    public void run() {
        BluetoothSocket socket = null;

        // Listen to the server socket if we're not connected
        Log.i("BluetoothComService", String.valueOf(mState));
        while (mState != STATE_CONNECTED) {
            try {
                // This is a blocking call and will only return on a
                // successful connection or an exception
                mAdapter.cancelDiscovery();

                socket = BluetoothAdapter.getDefaultAdapter()                           
                     .getRemoteDevice(getThermMAC())
                     .createRfcommSocketToServiceRecord(UUID
                     .fromString("00001101-0000-1000-8000-00805F9B34FB"));

           //   socket = mmServerSocket.accept();  // here socket might be closed or timeout
                Log.e("BluetoothComService", "No accept Exception");
            }catch (IOException e) {
                Log.e("TAG", e.getMessage());
            }

getThermMAC()以上是我的方法返回键合设备的地址。

getThermMAC() above is my method to return the address of the bonded device.

的run(),如果我用

socket=BluetoothAdapter.getDefaultAdapter()                           
                     .getRemoteDevice(getThermMAC())
                     .createRfcommSocketToServiceRecord(UUID
                     .fromString("00001101-0000-1000-8000-00805F9B34FB"));

然后我得到一个 NPE BluetoothSocket.java 501线在Android框架,是这行

then I get a NPE at line 501 of BluetoothSocket.java in the Android framework which is this line

int left = b.length;

所以,这样看来, B ,但它不似乎是经过调试。 B 字节[]这是从我的 BluetoothService 它发送一个全新的,初始化类字节[] 从在 ConnectedThread 正如SDK示例执行并作为我一直在做的旧版本。

So it would appear that b is null but it doesn't appear to be after debugging. b is a byte[] which is sent from my BluetoothService class which sends a brand new, initialized byte[] from within the ConnectedThread just as the sdk example does and as I've been doing on older versions.

如果我评论了这一点,并尝试使用

If I comment that out and try to use

socket = mmServerSocket.accept();

这在过去一直在努力,然后我得到

which has been working in the past, then I get

bt socket is not in listen state

所以,我不知道如何把它放在倾听状态,或者为什么它不会是。有没有人经历过这样或不知道如何解决办法呢?或者如何保持从获得 NPE 如果我用的第一个片段(如果是连正确的)。

So, I don't know how to put it in "listen state" or why it wouldn't be. Has anyone experienced this or know how to workaround it? Or how to keep from getting the NPE if I use the first snippet (if that is even correct).

我觉得

当我得到了一个 IOException异常我发现<一href="http://stackoverflow.com/questions/25550372/android-bluetoothserversockets-accept-mathos-throws-ioexception">this帖子害得我<一个href="http://stackoverflow.com/questions/11428820/bluetoothserversocket-accept-fails-and-throws-an-ioexception">here但这并没有得到我的任何地方。

When I was getting an IOException I found this post which led me here but this hasn't gotten me anywhere.

请注意:赏金消息称4.4.4但4.4.2上的Tab 4

Notice: The bounty message says 4.4.4 but it is 4.4.2 on the Tab 4

设备错误

我也注意到这些蓝牙错误,当我第一次在我的设备通过USB接口连接到电脑

I also notice these Bluetooth errors when I first connect my device to the computer through USB

09-05 15:18:03.217: E/BluetoothServiceJni(15148): SOCK FLAG = 0 ***********************
09-05 15:18:13.177: E/BluetoothServiceJni(15148): SOCK FLAG = 0 ***********************
09-05 15:18:13.217: E/BluetoothServiceJni(15148): SOCK FLAG = 0 ***********************

但我一直没能找出该标志的手段。

but I have not been able to find out what that flag means.

我知道有在BT协议栈4.x的已知缺陷(<一href="https://$c$c.google.com/p/android/issues/detail?id=63056&q=bluetooth%204.3%20unable%20to%20connect&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars"相对=nofollow>见的很多bug报告之一)

I realize there are known bugs in the BT stack 4.x (See one of many bug reports)

minSDK 正在10.不过,如果我找到一个可行的解决方案,然后我可以解决的。

minSDK is currently 10. Though, if I find a working solution then I can work around that.

推荐答案

尝试的这个的code,这个的正在承上启下7 <安卓4.4.2 / P>

Try this code, this is working android 4.4.2 on nexus 7

private boolean refreshDeviceCache(BluetoothGatt gatt){
    try {
        BluetoothGatt localBluetoothGatt = gatt;
        Method localMethod = localBluetoothGatt.getClass().getMethod("refresh", new Class[0]);
        if (localMethod != null) {
           boolean bool = ((Boolean) localMethod.invoke(localBluetoothGatt, new Object[0])).booleanValue();
            return bool;
         }
    } 
    catch (Exception localException) {
        Log.e(TAG, "An exception occured while refreshing device");
    }
    return false;
}


    public boolean connect(final String address) {
           if (mBluetoothAdapter == null || address == null) {
            Log.w(TAG,"BluetoothAdapter not initialized or unspecified address.");
                return false;
        }
            // Previously connected device. Try to reconnect.
            if (mBluetoothGatt != null) {
                Log.d(TAG,"Trying to use an existing mBluetoothGatt for connection.");
              if (mBluetoothGatt.connect()) {
                    return true;
               } else {
                return false;
               }
        }

        final BluetoothDevice device = mBluetoothAdapter
                .getRemoteDevice(address);
        if (device == null) {
            Log.w(TAG, "Device not found.  Unable to connect.");
            return false;
        }

        // We want to directly connect to the device, so we are setting the
        // autoConnect
        // parameter to false.
        mBluetoothGatt = device.connectGatt(MyApp.getContext(), false, mGattCallback));
        refreshDeviceCache(mBluetoothGatt);
        Log.d(TAG, "Trying to create a new connection.");
        return true;

}

这篇关于蓝牙连接不上4.4.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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