Bluetooth connection of mobile phone uses Jiabo printer (ZH380)
Bluetooth privileges
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-permission android:name="android.permission.BLUETOOTH" />
android 6.0 and above also need to add location permission
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
And dynamic authorization
public void applypermission(){ if(Build.VERSION.SDK_INT>=23){ int checkpermission= ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION); if(checkpermission!=PackageManager.PERMISSION_GRANTED){ ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1); } } } public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); for(int i=0;i<grantResults.length;i++){ if(grantResults[i]==PackageManager.PERMISSION_GRANTED){ Toast.makeText(MainActivity.this, permissions[i]+"Authorized",Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this,permissions[i]+"Deny authorization",Toast.LENGTH_SHORT).show(); } } }
Query Bluetooth device, turn on Bluetooth
private BluetoothAdapter adapter = null;
adapter = BluetoothAdapter.getDefaultAdapter();
if(adapter != null){ System.out.println("This machine has Bluetooth device!"); if(!adapter.isEnabled()){ Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivity(intent);//Turn on Bluetooth } else { Toast.makeText(MainActivity.this,"Bluetooth on",Toast.LENGTH_SHORT).show(); } } else{ Toast.makeText(MainActivity.this,"No Bluetooth",Toast.LENGTH_SHORT).show(); }
Sign up for radio, search for Bluetooth devices around you
View Bluetooth devices to bind
List<BluetoothDevice> bondDevices = new ArrayList<>();//Bluetooth device stored for binding
Set<BluetoothDevice> devices = adapter.getBondedDevices();//Get Bluetooth device to bind if(devices.size()>0){ for(Iterator<BluetoothDevice> it = devices.iterator();it.hasNext();){ BluetoothDevice device = (BluetoothDevice)it.next(); System.out.println(device.getAddress());//MAC address of Bluetooth device System.out.println(device.getName());//Bluetooth device name bondDevices.add(device); } }
Search the surrounding Bluetooth devices through broadcast
List<BluetoothDevice> unbondDevices = new ArrayList<>();//Store the searched Bluetooth device
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND); bluetoothReceiver = new BluetoothReceiver (); registerReceiver(bluetoothReceiver,intentFilter);//Sign up to start broadcasting
class BluetoothReceiver extends 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(BluetoothDevice.BOND_NONE==device.getBondState()) { System.out.println(device.getAddress()); System.out.println(device.getName()); unbondDevices.add(device); } } else if(adapter.ACTION_DISCOVERY_FINISHED.equals(action)){ System.out.println("complete"); } } }
Select Bluetooth device to bind
try { Method createBondMethod = BluetoothDevice.class.getMethod("createBond"); createBondMethod.invoke(device);//binding }catch (Exception e){ Toast.makeText(MainActivity.this,device.getName()+"Pairing failure- -",Toast.LENGTH_SHORT).show(); }
if(device.getBondState()==BluetoothDevice.BOND_BONDED){ //Query whether the binding is successful
}
Operate through the official SDK of Jiabo printer
Since the Jiabo printer used has an official user manual, you can follow the steps step by step
https://pan.baidu.com/s/1kfzEV55qGdF_0PKnWs_HSA data