BluetoothSocket.Connect()抛出IOException

时间:2013-12-27 13:26:48

标签: android bluetooth ioexception

我是Android新手,目前正致力于蓝牙应用。我必须发现并配对我成功完成的设备。之后,我必须与配对设备共享文件。 调用bluetoothSocket.connect()时出现问题。它在2.3中抛出了IO异常“服务发现失败”,并且“4.0及以上版本中的读取失败 - 套接字可能已关闭。我尝试了4.3。我的代码是:

private void sendDataToPairedDevice(String message ,BluetoothDevice device){       
byte[] toSend = message.getBytes();
BluetoothSocket socket=null;
OutputStream mmOutStream=null;
try {
    socket = device.createInsecureRfcommSocketToServiceRecord(getUUID());
} catch (IOException e) {
    Log.e("", "IOException during creating socket::: "+ e.getMessage());
}catch (NullPointerException e) {
    Log.e("", "NPE Exception during creating socket::: "+ e.getMessage());
}
bluetoothAdapter.cancelDiscovery();
CheckBlueToothState();
try {

    socket.connect();
    mmOutStream = socket.getOutputStream();
} catch ( IOException e ) {
    Log.e( "Bluetooth Socket", "IO Exception" );
    e.printStackTrace();
} catch ( NullPointerException e ) {
    Log.e( "Bluetooth Socket", "Null Pointer " );
}

try{
    if(mmOutStream==null){
        mmOutStream = socket.getOutputStream();
    }
    mmOutStream.write(toSend);
}catch (IOException e) {
    Log.e("", "IOException during write::: "+ e.getMessage());
}catch (NullPointerException e) {
    Log.e("", "NPE Exception during write::: "+ e.getMessage());
}

}

private UUID getUUID(){
    String android_id = Secure.getString(getApplicationContext()
            .getContentResolver(), Secure.ANDROID_ID);
    Log.i("System out", "android_id : " + android_id);

    final TelephonyManager tm = (TelephonyManager) getBaseContext()
            .getSystemService(Context.TELEPHONY_SERVICE);

    final String tmDevice, tmSerial, androidId;
    tmDevice = "" + tm.getDeviceId();
    Log.i("System out", "tmDevice : " + tmDevice);
    tmSerial = "" + tm.getSimSerialNumber();
    Log.i("System out", "tmSerial : " + tmSerial);
    androidId = ""
            + android.provider.Settings.Secure.getString(
                    getContentResolver(),
                    android.provider.Settings.Secure.ANDROID_ID);

    UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice
            .hashCode() << 32)
            | tmSerial.hashCode());
    return deviceUuid;
}

}

private void CheckBlueToothState() {
if( bluetoothAdapter == null ) {
    Log.e("","Bluetooth NOT supported" );
} else {
    if( bluetoothAdapter.isEnabled() ) {
        if( bluetoothAdapter.isDiscovering() ) {
            Log.e("", "Bluetooth is currently " +
                    "in device discovery process." );
        } else {
            Log.e("", "Bluetooth is Enabled." );
        }
    } else {
        Log.e("", "Bluetooth is NOT enabled" );
        Intent enableBtIntent = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE );
        startActivityForResult( enableBtIntent, REQUEST_ENABLE_BT );
    }
}

}

我在socket.connect()中获得IO异常。我在google和stackoverflow上搜索了很多,但没有一个解决方案有效。大多数解决方案都不使用硬编码的UUID,我不是很难编码。 任何帮助将受到高度赞赏。

0 个答案:

没有答案