0

运行下面的代码,我的 Galaxy 平板电脑 (SCH-I905 - Android 4.0.4) 正确地发现了 PC 上的三个蓝牙服务。在 Galaxy Nexus - Android 4.2.2 上运行相同的代码只能找到其中一项服务。任何想法表示赞赏:

private final BroadcastReceiver ServiceReceiver = new BroadcastReceiver()
{
    public void onReceive(Context context, Intent intent) 
    {
        String action = intent.getAction();
        if ( BluetoothDevice.ACTION_UUID.equals(action) )
        {
            BluetoothDevice device = intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
            Parcelable[] uuidExtra = intent.getParcelableArrayExtra("android.bluetooth.device.extra.UUID");

            for( int i = 0; i < uuidExtra.length; i++ )
            {
               Toast.makeText( this, device.getName() + ": " + uuidExtra[i].toString(), Toast.LENGTH_LONG).show();
            }
        }
    }

};

在 Eclipse 调试器中,以下数组显示了三个事件 [00001000-0000-1000-8000-00805f9b34fb、00001115-0000-1000-8000-00805f9b34fb、00001202-0000-1000-8000-1000-1000-8000-015805f90030b,但仅适用于平板电脑 [00001115-0000-1000-8000-00805f9b34fb -0000-1000-8000-00805f9b34fb] 用于 Nexus。不知道是版本问题还是硬件问题。谢谢你,约翰

最后一点 - 我正在使用... BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); device.fetchUuidsWithSdp();

4

1 回答 1

1

约翰

这似乎是一个Android版本的问题。使用与您的代码相似的htc渴望(Android 2.3)和无论我做什么都无法连接的联想平板电脑(Android 4.2.2),我得到了完全相同的结果。通过阅读一百万篇帖子,我到目前为止发现:

1)确保在尝试连接之前取消适配器上的发现 2)尝试使用反射,有些人声称这对他们有用 3)所以尝试这样的事情

Method m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class});
socket = (BluetoothSocket) m.invoke(device, 1);
bluetoothAdapter.cancelDiscovery();
socket.connect();
于 2014-04-22T17:11:20.910 回答