Android 无法连接蓝牙设备
我正在尝试连接蓝牙设备,希望向该设备发送数据,并希望从该设备接收数据 为了实现这一点,我遵循,但似乎我无法连接其他设备,因为在连接时,它抛出以下异常Android 无法连接蓝牙设备,android,bluetooth,bluetooth-socket,Android,Bluetooth,Bluetooth Socket,我正在尝试连接蓝牙设备,希望向该设备发送数据,并希望从该设备接收数据 为了实现这一点,我遵循,但似乎我无法连接其他设备,因为在连接时,它抛出以下异常 09-13 13:27:56.913: I/BluetoothConnect(2980): Connect exception:-java.io.IOException: [JSR82] connect: Connection is not created (failed or aborted). 我遵循的步骤。 启用蓝牙 Intent
09-13 13:27:56.913: I/BluetoothConnect(2980): Connect exception:-java.io.IOException: [JSR82] connect: Connection is not created (failed or aborted).
我遵循的步骤。
Intent turnOnIntent = new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOnIntent, REQUEST_ENABLE_BT);
Set<BluetoothDevice> bondSet = myBluetoothAdapter.getBondedDevices();
ArrayList<HashMap<String, String>> bondedhDevicesList = new ArrayList<HashMap<String, String>>();
for (Iterator<BluetoothDevice> it = bondSet.iterator(); it.hasNext();) {
BluetoothDevice bluetoothDevice = (BluetoothDevice) it.next();
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", bluetoothDevice.getName());
map.put("address", bluetoothDevice.getAddress());
bondedhDevicesList.add(map);
}
5.向设备写入数据
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 = new byte[1024]; // 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
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI activity
CreatePacket.mHandler.obtainMessage(MESSAGE_READ , bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.i("ConnectedThread", "while receiving data:-"+e.toString());
break;
}
}
}
public void write(byte[] bytes) {
Log.i("ConnectedThread", "data while writing:-"+bytes.toString());
try {
mmOutStream.write(bytes);
} catch (IOException e) {
Log.i("ConnectedThread", "while writing data to bluetooth:-"+e.toString());
}
}
若我仍然尝试写入then数据,那个么我将得到以下异常
请给我任何提示或参考
09-13 13:48:55.079: I/ConnectedThread(2980): while writing data to bluetooth:-java.io.IOException: socket closed
从过去三天开始,我一直在坚持这一点,但仍然没有得到任何解决方案 最好的方法是参考Android提供的示例聊天应用程序。这已经涵盖了所有必要的任务,如列出可用设备、建立连接、发送数据和接收等。您可以获得并参考这些任务。
这是我用来连接蓝牙模块的示例代码
public class OpenBluetoothPort extends AsyncTask<String, Void, BluetoothSocket> {
private final UUID SPP_UUID = UUID
.fromString("00001101-0000-1000-8000-00805F9B34FB");
private BluetoothAdapter mBluetoothAdapter;
private OnBluetoothPortOpened mCallback;
private BluetoothSocket mBSocket;
public interface OnBluetoothPortOpened {
public void OnBluetoothConnectionSuccess(BluetoothSocket socket);
public void OnBluetoothConnectionFailed();
}
public OpenBluetoothPort(Context context, OnBluetoothPortOpened callback) {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mCallback = callback;
}
@Override
protected BluetoothSocket doInBackground(String... params) {
if(mBluetoothAdapter.isEnabled()) {
try {
for(BluetoothDevice bt: mBluetoothAdapter.getBondedDevices()) {
if(bt.getName().equalsIgnoreCase(params[0])) {
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(bt.getAddress());
mBluetoothAdapter.cancelDiscovery();
mBSocket = device.createRfcommSocketToServiceRecord(SPP_UUID);
mBSocket.connect();
return mBSocket;
}
}
} catch(IOException e) {
if(mBSocket != null) {
try {
mBSocket.close();
} catch (IOException e1) {
Log.i("Bluetooth Close Exception","Error in closing bluetooth in OpenBluetoothPort.class");
e1.printStackTrace();
}
mBSocket = null;
}
Log.i("Bluetooth Connect Exception","Error in connecting in OpenBluetoothPort.class");
e.printStackTrace();
return null;
}
}
return null;
}
@Override
protected void onPostExecute(BluetoothSocket result) {
super.onPostExecute(result);
if(result != null && result.isConnected()) {
mCallback.OnBluetoothConnectionSuccess(result);
} else {
mCallback.OnBluetoothConnectionFailed();
}
}
}
公共类OpenBluetoothPort扩展异步任务{
专用最终UUID SPP_UUID=UUID
.fromString(“000011101-0000-1000-8000-00805F9B34FB”);
私人蓝牙适配器mBluetoothAdapter;
私有OnBluetoothPort打开mCallback;
私人蓝牙插座;
BluetoothPort上的公共接口已打开{
BluetoothConnectionSuccess上的公共无效(BluetoothSocket);
公共无效OnBluetoothConnectionFailed();
}
公共OpenBluetoothPort(上下文上下文,OnBluetoothPortOpen回调){
mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
mCallback=回调;
}
@凌驾
受保护的BluetoothSocket doInBackground(字符串…参数){
if(mBluetoothAdapter.isEnabled()){
试一试{
对于(BluetoothDevice bt:mBluetoothAdapter.getBondedDevices()){
if(bt.getName().equalsIgnoreCase(参数[0])){
BluetoothDevice=mBluetoothAdapter.getRemoteDevice(bt.getAddress());
mBluetoothAdapter.cancelDiscovery();
mBSocket=device.createrFComsockettoServiceRecord(SPP_UUID);
mBSocket.connect();
返回端口;
}
}
}捕获(IOE异常){
if(mBSocket!=null){
试一试{
mBSocket.close();
}捕获(IOE1异常){
Log.i(“蓝牙关闭异常”,“在OpenBluetoothPort.class中关闭蓝牙时出错”);
e1.printStackTrace();
}
mBSocket=null;
}
Log.i(“蓝牙连接异常”,“在OpenBluetoothPort.class中连接时出错”);
e、 printStackTrace();
返回null;
}
}
返回null;
}
@凌驾
受保护的void onPostExecute(BluetoothSocket结果){
super.onPostExecute(结果);
if(result!=null&&result.isConnected()){
mCallback.onBluetooth连接成功(结果);
}否则{
mCallback.OnBluetoothConnectionFailed();
}
}
}
09-13 13:48:55.079: I/ConnectedThread(2980): while writing data to bluetooth:-java.io.IOException: socket closed
public class OpenBluetoothPort extends AsyncTask<String, Void, BluetoothSocket> {
private final UUID SPP_UUID = UUID
.fromString("00001101-0000-1000-8000-00805F9B34FB");
private BluetoothAdapter mBluetoothAdapter;
private OnBluetoothPortOpened mCallback;
private BluetoothSocket mBSocket;
public interface OnBluetoothPortOpened {
public void OnBluetoothConnectionSuccess(BluetoothSocket socket);
public void OnBluetoothConnectionFailed();
}
public OpenBluetoothPort(Context context, OnBluetoothPortOpened callback) {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mCallback = callback;
}
@Override
protected BluetoothSocket doInBackground(String... params) {
if(mBluetoothAdapter.isEnabled()) {
try {
for(BluetoothDevice bt: mBluetoothAdapter.getBondedDevices()) {
if(bt.getName().equalsIgnoreCase(params[0])) {
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(bt.getAddress());
mBluetoothAdapter.cancelDiscovery();
mBSocket = device.createRfcommSocketToServiceRecord(SPP_UUID);
mBSocket.connect();
return mBSocket;
}
}
} catch(IOException e) {
if(mBSocket != null) {
try {
mBSocket.close();
} catch (IOException e1) {
Log.i("Bluetooth Close Exception","Error in closing bluetooth in OpenBluetoothPort.class");
e1.printStackTrace();
}
mBSocket = null;
}
Log.i("Bluetooth Connect Exception","Error in connecting in OpenBluetoothPort.class");
e.printStackTrace();
return null;
}
}
return null;
}
@Override
protected void onPostExecute(BluetoothSocket result) {
super.onPostExecute(result);
if(result != null && result.isConnected()) {
mCallback.OnBluetoothConnectionSuccess(result);
} else {
mCallback.OnBluetoothConnectionFailed();
}
}
}