简体   繁体   中英

How do I connect programatically the bluetooth(RN4020) with Android app?

I am developing an Android app that is going to communicate with target miroprocessor(ARM processor) board through a bluetooth device(RN4020). Since the pair PIN is random for each bluetooth module, I just want the app source code to connect the app with that RN4020 module (without pairing manually since I don't know the pair PIN). Also, I just want to know the UUID for RN4020 module, I have tried to find the UUID through some Android app it shows more than ten UUIDs. I am confused to choose the appropriate UUID.

Here is my code:

private static String address = "00:1E:C0:19:DB:A6";
private static final UUID MY_UUID = UUID.fromString("00002A00-0000-1000-8000-00805F9B34FB");


    connect.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View V)
        {
            Toast.makeText(getApplicationContext(), "Connecting to ...                  RN4020_D694", Toast.LENGTH_SHORT).show();
            if (mBluetoothAdapter.isEnabled())
                Connect();
        }
    });


public void Connect() {

    BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
    Toast.makeText(getApplicationContext(), "Connecting to ... " + device, Toast.LENGTH_SHORT).show();
    mBluetoothAdapter.cancelDiscovery();
    try {
        btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
        btSocket.connect();
        Toast.makeText(getApplicationContext(), "Connection made", Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
        try {
            btSocket.close();
        } catch (IOException e2) {
            changingText.setText("Unable to end the connection");
        }
        Log.d(TAG, "Socket creation failed");
    }

    beginListenForData();
}

When I tried to press the connect button, my app got stuck and it's not responding.

To avoid pairing you need to go for an insecure UUID rfcomm.

Replace:

btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);

with

btSocket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM