unable to make connection between 2 device bluetooth android

前端 未结 4 573
甜味超标
甜味超标 2021-01-31 04:02

I have tried to connect two devices by using the Android Bluetooth Chat example on the Android developer site. It\'s only connecting from one of the devices and when I am tryi

相关标签:
4条回答
  • 2021-01-31 04:14

    I also think your problem is not within your code.

    Two Years ago I have modified the BluetoothChat example application. You can find the modified project here - Bluetooth Chat and the binary here: Bluetooth Chat

    If this works better between your two devices it might help.

    I tested it just now on Android version 2.3.3 (Nexus One) and on Version 4.0.4 (Galaxy Nexus).

    0 讨论(0)
  • 2021-01-31 04:16

    I have done this,and i think you get connection lost when the device discoverable change. Please do check the device discoverable in both,you can enable it by code.

    Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,0);
    startActivity(discoverableIntent);
    
    0 讨论(0)
  • 2021-01-31 04:24

    I'm not sure if just copy the code straight away and try to run it if so, bear in mind android.app.ActionBar; require version 3.0. Also Bluetooth communication is not exactly peer to peer it's Master Slave.

    0 讨论(0)
  • 2021-01-31 04:29

    Saurabh, make this change please and watch it work:

                    if (Build.VERSION.SDK_INT < 9) { // VK: Build.Version_Codes.GINGERBREAD is not accessible yet so using raw int value
                    // VK: 9 is the API Level integer value for Gingerbread
                    try {
                        tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                } else {
                    Method m = null;
                    try {
                        m = device.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] { UUID.class });
                    } catch (NoSuchMethodException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    try {
                        tmp = (BluetoothSocket) m.invoke(device, (UUID) MY_UUID);
                    } catch (IllegalAccessException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
    
    0 讨论(0)
提交回复
热议问题