Question

I'm new to android bluetooth domain. I am able to list the paired bluetooth devices with UUID. My aim is to get the serial data via bluetooth and send it.

I have followed this reference link

My code :

import java.util.ArrayList;
import java.util.Iterator;
import com.example.discoverdevicesandservices.R;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Parcelable;
import android.widget.TextView;

public class DiscoverDevicesAndServices extends Activity {
TextView out;
private static final int REQUEST_ENABLE_BT = 1;
private BluetoothAdapter btAdapter; 
private ArrayList<BluetoothDevice> btDeviceList = new ArrayList<BluetoothDevice>();

*//** Called when the activity is first created. *//*
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

out = (TextView) findViewById(R.id.out);
//out.setMovementMethod(new ScrollingMovementMethod());

//Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothDevice.ACTION_UUID);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(ActionFoundReceiver, filter); 
// Getting the Bluetooth adapter
btAdapter = BluetoothAdapter.getDefaultAdapter();
out.append("\nAdapter: " + btAdapter);

CheckBTState();
}

This routine is called when an activity completes.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_ENABLE_BT) {
  CheckBTState();
}
}

 Override
 protected void onDestroy() {
 super.onDestroy();
 if (btAdapter != null) {
  btAdapter.cancelDiscovery();
 }
 unregisterReceiver(ActionFoundReceiver);
 }

  private void CheckBTState() {
 // Check for Bluetooth support and then check to make sure it is turned on
 // If it isn't request to turn it on
  // List paired devices
// Emulator doesn't support Bluetooth and will return null
 if(btAdapter==null) { 
  out.append("\nBluetooth NOT supported. Aborting.");
  return;
 } else {
  if (btAdapter.isEnabled()) {
    out.append("\nBluetooth is enabled...");

    // Starting the device discovery
    btAdapter.startDiscovery();
  } else {
    Intent enableBtIntent = new Intent(btAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
  }
}
}

private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver(){

@Override
public void onReceive(Context context, Intent intent) {
 String action = intent.getAction();
 if(BluetoothDevice.ACTION_FOUND.equals(action)) {
   BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
   out.append("\n  Device: " + device.getName() + ", " + device);
   btDeviceList.add(device);
 } else {
   if(BluetoothDevice.ACTION_UUID.equals(action)) {
     BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
     Parcelable[] uuidExtra =      intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
     for (int i=0; i<uuidExtra.length; i++) {
       out.append("\n  Device: " + device.getName() + ", " + device + ", Service: " + uuidExtra[i].toString());
     }
   } else {
     if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
       out.append("\nDiscovery Started...");
     } else {
       if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
         out.append("\nDiscovery Finished");
         Iterator<BluetoothDevice> itr = btDeviceList.iterator();
         while (itr.hasNext()) {
           // Get Services for paired devices
           BluetoothDevice device = itr.next();
           out.append("\nGetting Services for " + device.getName() + ", " + device);
           if(!device.fetchUuidsWithSdp()) {
             out.append("\nSDP Failed for " + device.getName());
           }

         }
       }
     }
   }
  }
 }
};

}

Please help!! Thanks.

Était-ce utile?

La solution

You have got the BluetoothDevice's details using the following part of your code, use this object to communicate to bluetooth device in serial port profile

"

BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

"

the object "device" is the bluetooth device that you want to connect to. In general the UUID for serial port profile is 00001101-0000-1000-8000-00805F9B34FB

> BluetoothSocket Bsock = null;
> private static final UUID MY_UUID_SPP=UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); >InputStream tmpIn = null; 
>OutputStream tmpOut = null;
> 
> 
> try{ 
>Bsock = device.createRfcommSocketToServiceRecord(MY_UUID_SPP); 
> Bsock.connect();
> tmpIn = socket.getInputStream(); 
>tmpOut = socket.getOutputStream(); 
>//use input stream and output stream to read and write to dataserially
> 
> }catch(IOException e){e.printStackTrace()}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top