因此,我想构建自己的应用,以便可以从非蜂窝平板电脑拨打Bluetooth
。在将设备与平板电脑配对之前,我已经制作了应用程序,但现在不知道下一步如何如何调用与平板电脑配对的设备?任何人都可以带头,这样我就可以开始工作。
public class MainActivity extends AppCompatActivity {
public static Switch bluetoothSwitch;
public static Button discoveryBtn;
public static Button searchBtn;
public static ListView listView;
public static ListView pairedListView;
public ArrayList<BluetoothDevice> btList;
public ArrayList<BluetoothDevice> pairList;
public ArrayAdapter<String> arrayAdapter;
public ArrayAdapter<String> arrayPairedAdapter;
public Set<BluetoothDevice> pairedDevices;
public BluetoothAdapter bluetoothAdapter; // For any Bluetooth activity, BluetoothAdapter is required.
public BroadcastReceiver mReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listview_new_devices);
pairedListView = findViewById(R.id.listView);
btList = new ArrayList<>();
pairList = new ArrayList<>();
arrayPairedAdapter = new ArrayAdapter<>(this,R.layout.device_name);
arrayAdapter = new ArrayAdapter<>(this,R.layout.device_name);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(deviceListener);
pairedListView.setAdapter(arrayPairedAdapter);
if(bluetoothAdapter!=null){
bluetoothSwitch = findViewById(R.id.bluetoothSwitch);
discoveryBtn = findViewById(R.id.discovery);
searchBtn = findViewById(R.id.search);
bluetoothSwitch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (bluetoothSwitch.isChecked()) {
turnOn();
} else {
turnOff();
}
}
});
discoveryBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
makeDiscoverable();
}
});
searchBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkPermission();
}
});
mReceiver = 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);
if(device.getBondState()!=BluetoothDevice.BOND_BONDED) {
String deviceName = device.getName();
String deviceAddress = device.getAddress();
btList.add(device);
arrayAdapter.add(deviceName + "\n" + deviceAddress);
}
}
else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
Log.i("Discovery status:","Finished");
}
else if(BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)){
final int curr_state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,BluetoothDevice.ERROR);
final int prev_state = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE,BluetoothDevice.ERROR);
if(curr_state == BluetoothDevice.BOND_BONDED && prev_state == BluetoothDevice.BOND_BONDING){
Toast.makeText(getApplicationContext(),"Paired",Toast.LENGTH_LONG).show();
}
else if(curr_state == BluetoothDevice.BOND_NONE && prev_state == BluetoothDevice.BOND_BONDED){
Toast.makeText(getApplicationContext(),"Unpaired",Toast.LENGTH_LONG).show();
}
}
}
};
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver, filter);
filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(mReceiver,filter);
// storing the paired devices information
pairedDevices = bluetoothAdapter.getBondedDevices();
if(pairedDevices.size()>0){
findViewById(R.id.title_paired).setVisibility(View.VISIBLE);
findViewById(R.id.listView).setVisibility(View.VISIBLE);
for(BluetoothDevice device:pairedDevices){
arrayPairedAdapter.add(device.getName()+"\n"+device.getAddress());
pairList.add(device);
}
}
else{
arrayPairedAdapter.add("No Devices found!!!");
}
}
}
public void searchDevices(){ // search for all available devices in the range of the bluetooth
if(bluetoothAdapter.isDiscovering()){
bluetoothAdapter.cancelDiscovery();
}
bluetoothAdapter.startDiscovery();
}
public void makeDiscoverable(){ // Make a device discoverable to all
Intent discoverableIntent =
new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 3000);
startActivity(discoverableIntent);
}
public void turnOn(){ // method to turn on the bluetooth
if (bluetoothAdapter == null) {
Toast.makeText(getApplicationContext(), "This device doesn't support Bluetooth", Toast.LENGTH_LONG).show();
} else {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1); // the second argument is for requestCode and it should be greater than 0.
Toast.makeText(getApplicationContext(), "Bluetooth is enabled", Toast.LENGTH_LONG).show();
}
}
public void turnOff(){ // method to turn off the bluetooth
bluetoothAdapter.disable();
Toast.makeText(getApplicationContext(),"Bluetooth is disabled", Toast.LENGTH_LONG).show();
}
public void checkPermission(){
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},1);
}
searchDevices();
}
private AdapterView.OnItemClickListener deviceListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(final AdapterView<?> parent, View view, final int position, long id) {
bluetoothAdapter.cancelDiscovery(); // we should stop the discovery.
Log.i("In listener","pairing device");
Log.i("Passing this device to ","pair method");
btList.get(position).createBond();
arrayPairedAdapter.notifyDataSetChanged();
}
};
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch(requestCode){
case 1:
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
searchDevices();
} else {
searchDevices();
}
break;
}
}
@Override
protected void onResume() {
super.onResume();
arrayAdapter.clear();
}
@Override
protected void onDestroy() {
super.onDestroy();
arrayAdapter.clear();
// Don't forget to unregister the ACTION_FOUND receiver.
unregisterReceiver(mReceiver);
bluetoothAdapter.cancelDiscovery(); // It's a big task for bluetooth adapter for discovery process and it will consume lot of resources,
// so, it's safe to cancel this process in onDestroy() lifecycle.
}
}