How to retrieve Bluetooth device info with Android Bluetooth device picker?

前端 未结 2 1686
谎友^
谎友^ 2021-02-04 19:34

Here is the test code that I am using :

public class IOConnectDirect extends Activity {

private static final String TAG = \"IOConnectDirect\";

private static f         


        
相关标签:
2条回答
  • 2021-02-04 20:12
    Try this code:  
    
    
    private ArrayAdapter<String> bondedAdapter = null,newScanedAdapter = null;   
    
    
    listViewPairedDevices.setAdapter(bondedAdapter);//oncreate
    listViewScanedDevices.setAdapter(newScanedAdapter);//oncreate
    
    public void doDiscovery(){
                setProgressBarIndeterminateVisibility(true);
                setTitle("Scanning..");
                textViewScanedDevices.setVisibility(View.VISIBLE);
                if(!BluetoothDemoActivity.bluetoothAdapter.isDiscovering()){
                    BluetoothDemoActivity.bluetoothAdapter.cancelDiscovery();
                }
                BluetoothDemoActivity.bluetoothAdapter.startDiscovery();
            }//doDiscovery() ends
    
    
        IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
                this.registerReceiver(myBroadcastReceiver, intentFilter);
    
                intentFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
                this.registerReceiver(myBroadcastReceiver, intentFilter);
    
        Set<BluetoothDevice> bondedSet = BluetoothDemoActivity.bluetoothAdapter.getBondedDevices();
                    Log.v(BluetoothDemoActivity.LOG, "BluetoothDemo : bondedSet: "+bondedSet);
    
                    int count = 0;
                    if(bondedSet.size() > 0){
                        for(BluetoothDevice device : bondedSet){
                            textViewPairedDevice.setVisibility(View.VISIBLE);
                            bondedAdapter.add(device.getName()+"\n"+device.getAddress());
                            Log.v(BluetoothDemoActivity.LOG, " count = "+count++);
                        }
                    }else{
                        bondedAdapter.add("No Devices");
                    }
    
    
    
        private BroadcastReceiver myBroadcastReceiver = new BroadcastReceiver() {
    
                @Override
                public void onReceive(Context context, Intent intent) {
                    // TODO Auto-generated method stub
                    String action = intent.getAction();
    
                    if(BluetoothDevice.ACTION_FOUND.equals(action)){
                        buttonScanScanDevices.setVisibility(View.GONE);
                        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                        if(device.getBondState()!= BluetoothDevice.BOND_BONDED){
                            newScanedAdapter.add(device.getName()+"\n"+device.getAddress());
                        }
                    }else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
                        setProgressBarIndeterminateVisibility(false);
                        setTitle("Select Device");
                        if(newScanedAdapter.getCount() == 0){
                            newScanedAdapter.add("no new device");
                        }
                    }else if(intent.getAction().equals("android.bluetooth.device.action.PAIRING_REQUEST")){
                        System.out.println("Pairing request came");
                        pair();
                    }
                }
            };//BroadcastReceiverends
    
    0 讨论(0)
  • 2021-02-04 20:13

    Try this code:

    private final BroadcastReceiver mBluetoothPickerReceiver = new BluetoothConnectActivityReceiver(this);
    
     void connectToService(String defaultAdapter) {
        if (defaultAdapter == null) {
            registerReceiver(mBluetoothPickerReceiver, new IntentFilter(BluetoothDevicePicker.ACTION_DEVICE_SELECTED));
            startActivity(new Intent(BluetoothDevicePicker.ACTION_LAUNCH)
                .putExtra(BluetoothDevicePicker.EXTRA_NEED_AUTH, false)
                .putExtra(BluetoothDevicePicker.EXTRA_FILTER_TYPE, BluetoothDevicePicker.FILTER_TYPE_ALL)
                .setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS));
        } else {
            mCardroid.getCardroidService().connectTo(defaultAdapter);
        }
    }
    
    public class BluetoothConnectActivityReceiver extends BroadcastReceiver {
        private BluetoothConnectActivity bluetoothConnectActivity;
     public BluetoothConnectActivityReceiver(BluetoothConnectActivity bluetoothConnectActivity) {
                this.bluetoothConnectActivity = bluetoothConnectActivity;
            }
    
         @Override
                public void onReceive(Context context, Intent intent)  {
                    if(BluetoothDevicePicker.ACTION_DEVICE_SELECTED.equals(intent.getAction())) {
                        context.unregisterReceiver(this);
                        BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                        bluetoothConnectActivity.connectToService(device.getAddress());
                    }
                }
            }
    

    The reference of complete code is :

    http://code.google.com/p/carbot/source/browse/trunk/src/net/cardroid/?r=8

    0 讨论(0)
提交回复
热议问题