我正在尝试连接到OBDsim并收集一些数据。
第0步。Paired my Android A7 and Linux Ubuntu 18.04's bluetooth adapter
步骤1。wget http://icculus.org/obdgpslogger/downloads/obdgpslogger-0.16.tar.gz
第2步。tar -zxvf obdgpslogger-0.16.tar.gz
cd obdgpslogger-0.16
mkdir build
cd build
第3步。sudo apt-get install libbluetooth-dev libfltk1.1-dev libfltk1.1 fltk1.1-doc fluid fftw3-dev libgps-dev libftdi-dev
cmake ..
make obdsim
cd ../bin/
第4步。sudo rfcomm bind 0 ##:##:##:##:##:## 1 //MAC address of linux PC
sudo sdptool add SP
第5步。./obdsim -b -g gui_fltk
最简单的代码
ArrayList<BluetoothDevice> DeviceList;
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();
if(pairedDevices.size() > 0)
{
for(BluetoothDevice device : pairedDevices)
{
DeviceList.add(device);
}
}
BluetoothDevice device = DeviceList.at(0); // it's my Linux Ubuntu Bluetooth device
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
BluetoothSocket socket;
try
{
socket = device.createInsecureRfcommSocketToServiceRecord(uuid);
}
catch(IOException e)
{
e.printStackTrace();
}
try
{
socket.connect();
}
catch (IOException e)
{
e.printStackTrace();
return 0;
}
套接字正在创建,没有任何异常,但是
它总是在socket.connet();
上显示java.io.IOException: read failed, socket might closed or timeout, read ret: -1
我也尝试过createRfcommSocketToServiceRecord
,但是socket.connect()
还是失败了。
我做错了什么?请帮忙。