[英]Pair android device by bluetooth by entering PIN programmatically
I used this library https://github.com/akexorcist/Android-BluetoothSPPLibrary to send and get messages from a microcontroller . 我使用此库https://github.com/akexorcist/Android-BluetoothSPPLibrary从微控制器发送和获取消息。 but it needs to be paired with the device .
但是它需要与设备配对。 i want to do it programmatically and enter pin from code .
我想以编程方式执行此操作,然后从代码中输入pin。 any suggestion ?
有什么建议吗?
Edited : 编辑:
public class TerminalActivity extends Activity { BluetoothSPP bt; 公共类TerminalActivity扩展了Activity {BluetoothSPP bt;
TextView textStatus, textRead;
EditText etMessage;
Menu menu;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_terminal);
Log.i("Check", "onCreate");
textRead = (TextView)findViewById(R.id.textRead);
textStatus = (TextView)findViewById(R.id.textStatus);
etMessage = (EditText)findViewById(R.id.etMessage);
bt = new BluetoothSPP(this);
if(!bt.isBluetoothAvailable()) {
Toast.makeText(getApplicationContext()
, "Bluetooth is not available"
, Toast.LENGTH_SHORT).show();
finish();
}
bt.setOnDataReceivedListener(new BluetoothSPP.OnDataReceivedListener() {
public void onDataReceived(byte[] data, String message) {
textRead.append(message + "\n");
}
});
bt.setBluetoothConnectionListener(new BluetoothSPP.BluetoothConnectionListener() {
public void onDeviceDisconnected() {
textStatus.setText("Status : Not connect");
menu.clear();
getMenuInflater().inflate(R.menu.menu_connection, menu);
}
public void onDeviceConnectionFailed() {
textStatus.setText("Status : Connection failed");
}
public void onDeviceConnected(String name, String address) {
textStatus.setText("Status : Connected to " + name);
menu.clear();
getMenuInflater().inflate(R.menu.menu_disconnection, menu);
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
getMenuInflater().inflate(R.menu.menu_connection, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id == R.id.menu_android_connect) {
bt.setDeviceTarget(BluetoothState.DEVICE_ANDROID);
/*
if(bt.getServiceState() == BluetoothState.STATE_CONNECTED)
bt.disconnect();*/
Intent intent = new Intent(getApplicationContext(), DeviceList.class);
startActivityForResult(intent, BluetoothState.REQUEST_CONNECT_DEVICE);
} else if(id == R.id.menu_device_connect) {
bt.setDeviceTarget(BluetoothState.DEVICE_OTHER);
/*
if(bt.getServiceState() == BluetoothState.STATE_CONNECTED)
bt.disconnect();*/
Intent intent = new Intent(getApplicationContext(), DeviceList.class);
startActivityForResult(intent, BluetoothState.REQUEST_CONNECT_DEVICE);
} else if(id == R.id.menu_disconnect) {
if(bt.getServiceState() == BluetoothState.STATE_CONNECTED)
bt.disconnect();
}
return super.onOptionsItemSelected(item);
}
public void onDestroy() {
super.onDestroy();
bt.stopService();
}
public void onStart() {
super.onStart();
if (!bt.isBluetoothEnabled()) {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, BluetoothState.REQUEST_ENABLE_BT);
} else {
if(!bt.isServiceAvailable()) {
bt.setupService();
bt.startService(BluetoothState.DEVICE_ANDROID);
setup();
}
}
}
public void setup() {
Button btnSend = (Button)findViewById(R.id.btnSend);
btnSend.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if(etMessage.getText().length() != 0) {
bt.send(etMessage.getText().toString(), true);
etMessage.setText("");
}
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == BluetoothState.REQUEST_CONNECT_DEVICE) {
if(resultCode == Activity.RESULT_OK)
bt.connect(data);
} else if(requestCode == BluetoothState.REQUEST_ENABLE_BT) {
if(resultCode == Activity.RESULT_OK) {
bt.setupService();
bt.startService(BluetoothState.DEVICE_ANDROID);
setup();
} else {
Toast.makeText(getApplicationContext()
, "Bluetooth was not enabled."
, Toast.LENGTH_SHORT).show();
finish();
}
}
}
} }
thanks in advance 提前致谢
Did you try calling createBond()
through reflection? 您是否尝试通过反射调用
createBond()
?
This works for me, with device
being a BluetoothDevice
: 这对我有用,
device
是BluetoothDevice
:
Method m = device.getClass().getMethod("createBond", (Class[]) null);
m.invoke(device, (Object[]) null);
EDIT: Also, if you want to set a particular pin, this should work: 编辑:此外,如果您想设置一个特定的引脚,这应该工作:
byte[] pinBytes = convertPinToBytes("0000");
Method m = device.getClass().getMethod("setPin", byte[].class);
m.invoke(device, pinBytes);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.