android 蓝牙 service discovery failed 解决
16lz
2021-01-26
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;
}
更多相关文章
- android 蓝牙发送数据
- android文件打开方式
- android打开照相机的方法
- 蓝牙遥控器获取电量GATT
- Android和iOS如何连接指定的蓝牙设备
- android 实现抽屉效果
- Android(安卓)蓝牙写入无回调问题
- Android应用程序漏洞防护措施打开的正确方式!
- Android(安卓)Studio 将github作为远程maven仓库