我有蓝牙遥控器(我想自拍杆)通过蓝牙连接我的手机。它可以工作,它可以拍照但我想从我的应用程序中的遥控器接收信号来远程控制音乐。
问题:在我创建套接字后,我无法连接到它而没有IOException。
以下是所有代码:
public class MainActivity extends Activity {
BluetoothAdapter btAdapter;
public static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
protected static final int SUCCESS_CONNECT = 0;
protected static final int MESSAGE_READ = 1;
IntentFilter filter;
String tag = "debugging";
Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
Log.i(tag, "in handler");
super.handleMessage(msg);
switch(msg.what){
case SUCCESS_CONNECT:
// DO something
ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket)msg.obj);
Toast.makeText(getApplicationContext(), "CONNECT", 0).show();
String s = "successfully connected";
connectedThread.write(s.getBytes());
Log.i(tag, "connected");
break;
case MESSAGE_READ:
byte[] readBuf = (byte[])msg.obj;
String string = new String(readBuf);
Toast.makeText(getApplicationContext(), string, 0).show();
break;
}
}
};
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
btAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice device = getDeviceByName("AB Shutter 3");
ConnectThread connect = new ConnectThread(device);
connect.start();
} catch (Exception e) {
e.printStackTrace();
}
}
private BluetoothDevice getDeviceByName(String printerName)
{
Set<BluetoothDevice> pairedDevices = BluetoothAdapter.getDefaultAdapter().getBondedDevices();
BluetoothDevice remoteDevice;
for (BluetoothDevice device : pairedDevices)
{
if (device.getName() == null)
continue;
if (device.getName().contains(printerName))
{
Log.e("","device name: "+device.getName());
remoteDevice = device;
return remoteDevice;
}
}
return null;
}
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
// Use a temporary object that is later assigned to mmSocket,
// because mmSocket is final
BluetoothSocket tmp = null;
mmDevice = device;
Log.i(tag, "construct");
// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
// MY_UUID is the app's UUID string, also used by the server code
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
Log.i(tag, "get socket failed");
e.printStackTrace();
}
mmSocket = tmp;
}
public void run() {
try {
// Cancel discovery because it will slow down the connection
btAdapter.cancelDiscovery();
Log.i(tag, "connect - run");
mmSocket.connect();
Log.i(tag, "connect - succeeded");
} catch (Exception connectException) {
Log.i(tag, "connect failed");
// Unable to connect; close the socket and get out
connectException.printStackTrace();
return;
}
// Do work to manage the connection (in a separate thread)
mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();
}
}
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer; // buffer store for the stream
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
buffer = new byte[1024];
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI activity
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (Exception e) {
e.printStackTrace();
break;
}
}
}
public void write(byte[] bytes) {
try {
mmOutStream.write(bytes);
} catch (IOException e) { }
}
}
}
错误在第131行,ConnectThread run()
try {
// Cancel discovery because it will slow down the connection
btAdapter.cancelDiscovery();
Log.i(tag, "connect - run");
mmSocket.connect(); //Here it fails
Log.i(tag, "connect - succeeded");
} catch (Exception connectException) {
Log.i(tag, "connect failed");
// Unable to connect; close the socket and get out
connectException.printStackTrace();
return;
}
错误显示:
之前是否有人遇到此错误并且可以指向正确的方向?
答案 0 :(得分:0)
这不是错误,这是一个警告。警告发生后,您是否正在丢失某种功能?如果没有,听起来就像你无法控制它,但根据这个答案中的几个人也不用担心:https://stackoverflow.com/a/24229604/3299157