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;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值