AMI amitekh
AMI amitekh

Reputation: 198

How to get connected Bluetooth device data in android?

I'm developing an app to receive Bluetooth commands from Bluetooth remote. So the requirement is whenever user clicks a button, I should toast that button Key_Code. I've tried to access the Bluetooth_Service through BluetoothManager class, but there is no access to that part. Please do help me.

MyCode to getBluetoothDevices:

Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
            String str = "";
            for (BluetoothDevice device : pairedDevices) {
                mDevice = device;
                str = str+mDevice.getName()+", ";
            }              
        }else{
            System.out.println("No devices Found");
        }

I'm getting the device name and mac address etc, but there isn't a binder for the receiving of commands

Upvotes: 1

Views: 927

Answers (1)

V Jayakar Edidiah
V Jayakar Edidiah

Reputation: 976

There are two ways to do that.

  1. If you want to receive the KEY_CODES of android remote from your Activity, you can directly call the below method. It will give you all the keycodes of the buttons pressed.

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Toast.makeText(this,"KeyCode is"+keyCode,Toast.LENGTH_LONG).show();          
        return super.onKeyDown(keyCode, event);
    }
    

Note: This cannot be possible with an android service.

  1. Write a Bluetooth Socket that connects with Your own BluetoothServer and Start communicating.

Client:

private class ClietnThread extends Thread{

    ClietnThread(BluetoothDevice dev){
        mDevice= dev;
    }

    @Override
    public void run() {
        super.run();
        Log.e(Tag,"Now CONNECTING to"+mDevice.getName());
        try{
            //mBluetoothSocket =(BluetoothSocket) mDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(mDevice,1);
            mBluetoothSocket = mDevice.createRfcommSocketToServiceRecord(MY_UUID);
            mBluetoothSocket.connect();
        }catch (IOException e){
            Log.e(Tag,"Connect Failed");
            e.printStackTrace();
            try {
                mBluetoothSocket.close();
            } catch (IOException closeException) {
                Log.e("Client", "Could not close the client socket", closeException);
            }
        }
    }
}

Server:

private class MyServer extends Thread{
        MyServer(){
        }

    @Override
    public void run() {
        super.run();
        Log.e(Tag,"Bluetooth Listening");
        try {
            mBluetoothServerSocket =  mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
            mBluetoothAdapter.cancelDiscovery();
        }catch (IOException e){
            e.printStackTrace();
        }
        while (true){
            try{
               BluetoothSocket mSoicket = mBluetoothServerSocket.accept();
               Log.e(Tag,"ConnectedToSpike");
            }catch (IOException e){
               e.printStackTrace();
            }
        }
    }
}

Upvotes: 1

Related Questions