android 藍牙 service discovery failed 解決

public static String ErrorMessage="No Error";
private static final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private static BluetoothAdapter myBluetoothAdapter;
private static BluetoothDevice myDevice;
private static BluetoothSocket mySocket = null;
private static OutputStream myOutStream = null;
private static InputStream myInStream = null;



public static boolean SPPOpen(BluetoothAdapter bluetoothAdapter, BluetoothDevice btDevice)

{
boolean error=false;
myBluetoothAdapter = bluetoothAdapter;
myDevice = btDevice;
BluetoothSocket tmp=null;


if(!myBluetoothAdapter.isEnabled())
{
ErrorMessage = "藍牙適配器沒有打開";
       return false;
}


try 
{
            Method m = myDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class});
            try
            {
            tmp = (BluetoothSocket)m.invoke(myDevice, Integer.valueOf(1));
            }catch(Exception e)
            {
           
            }
        }catch(NoSuchMethodException e)
        {
        }


        mySocket = tmp;
        myBluetoothAdapter.cancelDiscovery();


        try 
{
mySocket.connect();

catch (IOException e2) 
{
ErrorMessage = e2.getLocalizedMessage();//"無法連接藍牙打印機";
mySocket = null;
return false;
}


try 
{
myOutStream = mySocket.getOutputStream();

catch (IOException e3) 
{
myOutStream = null;
error = true;
}


try 
{
myInStream = mySocket.getInputStream();

catch (IOException e3) 
{
myInStream = null;
error = true;
}


if(error)
{
SPPClose();
return false;
}

return true;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章