I am designing this app for api level 2.2
I want pair with a available bluetooth device and then listen the ststus, I have done that by the following way:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if(btAdapter.isDiscovering()){
btAdapter.cancelDiscovery();
}
if(!listAdapter.getItem(i).contains("Paired")){
try {
BluetoothDevice selectedDevice = devices.get(i);
pairDevice(selectedDevice);
Intent intent=new Intent(getBaseContext(),ConnectedBtActivity.class);
intent.putExtra("MAC",selectedDevice.getAddress());
startActivity(intent);
}
catch (Exception e){}
}
else{
BluetoothDevice selectedDevice = devices.get(i);
Intent intent=new Intent(getBaseContext(),ConnectedBtActivity.class);
intent.putExtra("MAC",selectedDevice.getAddress());
startActivity(intent);
}
}
});
private void pairDevice(BluetoothDevice device) {
try {
Method m = device.getClass().getMethod("createBond", (Class[]) null);
m.invoke(device, (Object[]) null);
} catch (Exception e) {
Toast.makeText(getBaseContext(),"Exception: "+e.getMessage(),Toast.LENGTH_LONG ).show();
}
}
Here listView contain all the available devices, listAdapter is an ArrayAdapter which contain the available device name and for pair device concate "(Paired)" with device name. You can clearly see I want to open a new activity if the device already paired and if not paired then initiate the pairing process. Now issue is that pairDevice() is a multi threaded process that means does not wait until the pairing is complete. I want to listen whether the pairing is done or not after that new activity should open. For better clarification I am posting full code:
public class MyBluetoothScanActivity extends AppCompatActivity{
Button bt,bt_count;
ListView listView;
BluetoothAdapter btAdapter;
Set<BluetoothDevice> devicesArray;
public static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
IntentFilter filter;
BroadcastReceiver receiver;
ArrayAdapter<String> listAdapter;
ArrayList<String> pairedDevices;
ArrayList<BluetoothDevice> devices;
int count=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_bluetooth_scan);
bt=(Button) findViewById(R.id.bT_scan2);
bt.setTransformationMethod(null);
bt_count=(Button) findViewById(R.id.bt_count);
bt_count.setTransformationMethod(null);
bt_count.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getBaseContext(),"Count: "+count,Toast.LENGTH_SHORT ).show();
}
});
listView=(ListView) findViewById(R.id.listViewscan);
btAdapter = BluetoothAdapter.getDefaultAdapter();
filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
if(!btAdapter.isEnabled()){
turnOnBT();
}
init();
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
newScan();
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if(btAdapter.isDiscovering()){
btAdapter.cancelDiscovery();
}
if(!listAdapter.getItem(i).contains("Paired")){
try {
BluetoothDevice selectedDevice = devices.get(i);
pairDevice(selectedDevice);
Intent intent=new Intent(getBaseContext(),ConnectedBtActivity.class);
intent.putExtra("MAC",selectedDevice.getAddress());
startActivity(intent);
}
catch (Exception e){}
}
else{
BluetoothDevice selectedDevice = devices.get(i);
Intent intent=new Intent(getBaseContext(),ConnectedBtActivity.class);
intent.putExtra("MAC",selectedDevice.getAddress());
startActivity(intent);
}
}
});
}
private void newScan(){
btAdapter.cancelDiscovery();
Toast.makeText(getBaseContext(),"New Scan Start",Toast.LENGTH_SHORT ).show();
listAdapter= new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1,0);
listView.setAdapter(listAdapter);
devices = new ArrayList<BluetoothDevice>();
btAdapter.startDiscovery();
}
private void getPairedDevices() {
devicesArray = btAdapter.getBondedDevices();
if(devicesArray.size()>0){
for(BluetoothDevice device:devicesArray){
pairedDevices.add(device.getName());
}
}
}
void turnOnBT(){
Intent intent =new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(intent);
}
void init(){
receiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Toast.makeText(getBaseContext(),"new br: "+action,Toast.LENGTH_LONG ).show();
if(BluetoothDevice.ACTION_FOUND.equals(action)){
pairedDevices=new ArrayList<String>();
getPairedDevices();
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Toast.makeText(getBaseContext(),"Dev: "+device.getName(),Toast.LENGTH_LONG ).show();
devices.add(device);
String s = "";
for(int a = 0; a < pairedDevices.size(); a++){
if(device.getName().equals(pairedDevices.get(a))){
//append
s = "(Paired)";
break;
}
}
listAdapter.add(device.getName()+" "+s+" "+"\n"+device.getAddress());
}
else if(BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)){
if(btAdapter.getState() == btAdapter.STATE_OFF){
turnOnBT();
}
}
}
};
registerReceiver(receiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
registerReceiver(receiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(receiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(receiver, filter);
}
private void pairDevice(BluetoothDevice device) {
try {
Method m = device.getClass().getMethod("createBond", (Class[]) null);
m.invoke(device, (Object[]) null);
} catch (Exception e) {
Toast.makeText(getBaseContext(),"Exception: "+e.getMessage(),Toast.LENGTH_LONG ).show();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
try {
Toast.makeText(getBaseContext(),"Un registration",Toast.LENGTH_SHORT ).show();
unregisterReceiver(receiver);
}
catch (Exception e){}
}
}
您还可以注册ACTION_BOND_STATE_CHANGED广播,并检查其额外字段以检查绑定过程的结果。
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.