我正在尝试连接两部手机而不使用private void onStarClicked(DatabaseReference postRef) {
postRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Post p = mutableData.getValue(Post.class);
if (p == null) {
return Transaction.success(mutableData);
}
if (p.stars.containsKey(getUid())) {
// Unstar the post and remove self from stars
p.starCount = p.starCount - 1;
p.stars.remove(getUid());
} else {
// Star the post and add self to stars
p.starCount = p.starCount + 1;
p.stars.put(getUid(), true);
}
// Set value and report transaction success
mutableData.setValue(p);
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b,
DataSnapshot dataSnapshot) {
// Transaction completed
Log.d(TAG, "postTransaction:onComplete:" + databaseError);
}
});
}
方法无法连接设备进行配对,它会抛出IOException。 createInsecureRfcommSocketToServiceRecord
你可以看到我的代码。在App中有新的扫描蓝牙设备列表,当我点击该特定项目时,它会转到第二个活动,并且它与该设备连接。
代码:
此OnItemClick是搜索新设备列表的列表。
System.err: java.io.IOException: socket closed
SecondActivity.class:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
bdDevice = arrayListBluetoothDevices.get(position);
Intent intents = new Intent(Main_Activity.this, SecondActivity.class);
intents.putExtra("deviceAddress", bdDevice.getAddress());
startActivity(intents);
}
我在@Override
public void onResume() {
super.onResume();
Intent intent = getIntent();
address = intent.getStringExtra("deviceAddress");
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address);
try {
//I am creating insecureRFcomm because of no pairing dialog required.
socket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
socket.getRemoteDevice().getAddress();
} catch (Exception e) {
e.printStackTrace();
}
bluetoothAdapter.cancelDiscovery();
Log.d(TAG, "...Connecting to Remote...");
try {
//Connection establish between BT-Device and phone.
socket.connect();
counter = 0;
Log.d(TAG, "...Connection established and data link opened...");
} catch (IOException e) {
try {
//Connection close.
socket.close();
if (inStream!=null){
inStream.close();
}
} catch (Exception e2) {
e.printStackTrace();
e2.printStackTrace();
}
finally {
final AlertDialog alertDialog = new AlertDialog.Builder(SBLActivity.this).create();
alertDialog.setMessage("Device is Non SBL.");
alertDialog.setTitle("Device Connection");
alertDialog.setCancelable(false);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog.dismiss();
finish();
}
});
alertDialog.show();
}
}
}
收到错误并且它会阻止阻塞。如何修复,或任何其他方式连接蓝牙插座没有配对对话或配对设备或与配对设备和没有配对对话
谢谢。