Android Bluetooth Paring Input Device -
i trying make pairing code , works normal devices. if use bluetooth scanner pairs device doesn't work till go android settings , select input device checkbox. how can code?
thank you.
here pairing code:
public static bluetoothsocket createl2capbluetoothsocket(string address, int psm){ return createbluetoothsocket(type_l2cap, -1, false,false, address, psm); } // method creating bluetooth client socket private static bluetoothsocket createbluetoothsocket( int type, int fd, boolean auth, boolean encrypt, string address, int port){ try { constructor<bluetoothsocket> constructor = bluetoothsocket.class.getdeclaredconstructor( int.class, int.class,boolean.class,boolean.class,string.class, int.class); constructor.setaccessible(true); bluetoothsocket clientsocket = (bluetoothsocket) constructor.newinstance(type,fd,auth,encrypt,address,port); return clientsocket; }catch (exception e) { return null; } } private void dopair(final bluetoothdevice device, final int devicetag) { try { method method = device.getclass().getmethod("createbond", (class[]) null); method.invoke(device, (object[]) null); } catch (exception e) { log.e(log_tag, "cannot pair device", e); } broadcastreceiver mreceiver = new broadcastreceiver() { @override public void onreceive(context context, intent intent) { string action = intent.getaction(); if (bluetoothdevice.action_bond_state_changed.equals(action)) { final int state = intent.getintextra( bluetoothdevice.extra_bond_state, bluetoothdevice.error); final int prevstate = intent.getintextra( bluetoothdevice.extra_previous_bond_state, bluetoothdevice.error); if (state == bluetoothdevice.bond_bonded && prevstate == bluetoothdevice.bond_bonding) { log.d(log_tag, "paired new device"); if (listener != null) { listener.onbluetoothpaireddevicechange(bluetoothadapter .getbondeddevices().iterator().next() .getname(), devicetag); } context.unregisterreceiver(this); } else if (state == bluetoothdevice.bond_none && prevstate == bluetoothdevice.bond_bonding) { log.d(log_tag, "cannot pair new device"); if (listener != null) { listener.onbluetoothstatuschange(bluetoothhelper.idle_status); } context.unregisterreceiver(this); } } } }; intentfilter intent = new intentfilter( bluetoothdevice.action_bond_state_changed); context.registerreceiver(mreceiver, intent); }
first,if target version after api 19 , can use bluetoothdevice.createbond()
pair device directly without use reflection. following methods work on devices before android 6.0!(after 6.0, system caller check avoid outer app call these methods)
bluetoothadapter.getdefaultadapter().getprofileproxy(mainactivity.this, new bluetoothprofile.servicelistener() { @override public void onserviceconnected(int profile, bluetoothprofile proxy) { class<?> clazz = null; try { clazz = class.forname("android.bluetooth.bluetoothinputdevice"); object obj = clazz.cast(proxy); method connectmethod = clazz.getdeclaredmethod("connect",bluetoothdevice.class); boolean resultcode = (boolean) connectmethod.invoke(obj,device); method setpriority = clazz.getdeclaredmethod("setpriority",bluetoothdevice.class,int.class); setpriority.invoke(obj,device,1000); } catch (classnotfoundexception e) { e.printstacktrace(); } catch (nosuchmethodexception e) { e.printstacktrace(); } catch (invocationtargetexception e) { e.printstacktrace(); } catch (illegalaccessexception e) { e.printstacktrace(); } } @override public void onservicedisconnected(int profile) { log.d("wtf","onservice disconnected "+profile); } },input_device); final int input_device = 4; // hidden memeber in bluetoothdevice
Comments
Post a Comment