2

I found a sample code for manage connection on service. But I don't know how use it.

I only write here the code that I don't understand, to see all the code:

Link Here

CODE

public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d("popopo", "Onstart Command");
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter != null) {
        device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        deviceName = device.getName();
        String macAddress = device.getAddress();
        if (macAddress != null && macAddress.length() > 0) {
            connectToDevice(macAddress);
        } else {
            stopSelf();
            return 0;
        }
    }
    String stopservice = intent.getStringExtra("stopservice");
    if (stopservice != null && stopservice.length() > 0) {
        stop();
    }
    return START_STICKY;
}

I connect correcly to bluetooth without service, getting device with this code:

if (BluetoothDevice.ACTION_FOUND.equals(action)) {
    // Get the BluetoothDevice object from the Intent
    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    // Add the name and address to an array adapter to show in a ListView
    mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}

My question is: How to pass my found Bluetooth device ID when onStartCommand() gonna executed?

Which Bluetooth Device it's trying to connect when I don't executed ACTION_FOUND?

Community
  • 1
  • 1
Jose M.
  • 219
  • 1
  • 2
  • 11

1 Answers1

0

May be a bit late, but just in case. If I recall correctly, there are a number of ways you can go to connect to a device, given its mac address.

  • Pass the device's address as an extra in the creation intent.

    In UI Activity

    Intent intent=new Intent(this,BluetoothService.class));
    intent.putExtra("DEVICE_ADDRESS",device.getAddress());
    
    startService(intent);
    

    In Bluetooth service

    public int onStartCommand(Intent intent, int flags, int startId) {
        String address = intent.getExtras("DEVICE_ADDRESS");
    
        ###DO SOMETHING WITH ADDRESS HERE###
    }
    
  • Create a 'connectToDevice(String address)' method in your service, and call it from wherever you have your BroadcastReceiver.

    In Bluetooth service

    protected synchronized void connectToDevice(BluetoothDevice device){//Cancells any threads attempting to connect and starts a new connectThread (new connection)
    
        if (mConnectThread != null) {
            mConnectThread.cancel();
            mConnectThread = null;
            Log.d(TAG,"Cancelled connectThread");
        }
    
    
        if (mConnectedThread != null) {
            mConnectedThread.cancel();
            mConnectedThread = null;
            Log.d(TAG,"Cancelled connectedThread");
    
        }
    
        mConnectThread = new ConnectThread(device);
        mConnectThread.start();
        Log.d(TAG,"Starting connectThread");
    }
    

    In Broadcast receiver

    if (BluetoothDevice.ACTION_FOUND.equals(action)) {
        // Get the BluetoothDevice object from the Intent
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        // Add the name and address to an array adapter to show in a ListView
        mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
        BluetoothService mBluetoothService = new BluetoothService();
        mBluetoothService.connectToDevice(device.getAddress());
    
    }
    

You should check out the BluetoothChat example offered by android. I think it shows a lot of tings you'll need.