简体   繁体   中英

How to discover the Bluetooth device in Android?

I am developing an application where I have to connect to Bluetooth device. I use "adb push" to push my apk to android x86. The bluetooth of Android-x86 is normal,and it can scan the bluetooth device.

I use two button here. One of the button call "scan", it list the device that I have paired. another one call discover,it work is scan the bluetooth device.

Now I can use the "scan button" to list the device that I have paired. But when I want to use the "discover" button to scan the device by my app, it always crash when I type the code into btn_discover.setOnClickListener:

what should I do?

Here is my code.

public class Main extends Activity {

private BluetoothAdapter mBluetoothAdapter;
private static final int REQUEST_SELECT_DEVICE = 1;
private Button btn_scan;
private Button btn_discover;
private TextView pair_list;
private TextView scan_list;


private Set<BluetoothDevice> pairedDevices;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btn_scan = (Button)findViewById(R.id.btn_scan);
    pair_list = (TextView)findViewById(R.id.pair_list);



    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter == null)
    {
        Toast.makeText(this, "No support bluetooth", Toast.LENGTH_SHORT).show();
        return;
    }else if(mBluetoothAdapter != null)     {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_SELECT_DEVICE);
    }


    //******************scan按鈕動作-將已配對過的藍芽裝置列出來
    btn_scan.setOnClickListener(new OnClickListener() {     
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            pairedDevices = mBluetoothAdapter.getBondedDevices();
            if(pairedDevices.size()>0)  {
                for(BluetoothDevice bDevice : pairedDevices)  {
                    pair_list.append(bDevice.getName() + "\n" + bDevice.getAddress() + "\n" + bDevice.getBondState() + "\n" );
                }
            }
        }
    });
    //******************scan按鈕動作結束


    btn_discover.setOnClickListener(new OnClickListener() {     
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
            registerReceiver(mReceiver, filter);
        }
    });


}


protected void onDestroy() {

super.onDestroy();
if (mBluetoothAdapter != null) {
    mBluetoothAdapter.cancelDiscovery();
    }
unregisterReceiver(mReceiver);
}


private final BroadcastReceiver mReceiver = 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)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            scan_list.append(device.getName() + "\n" + device.getAddress() + "\n");
        }
    }
};






@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

} `

I solve it.

I add a new xml file name device_name .

And modify the code of the following:

newDevicelistArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);

And It can show the bluetooth device by ListView.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM