- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的程序与充当服务器的嵌入式蓝牙设备连接。我成功找到了附近的蓝牙设备,但是当我尝试连接到新设备时,我的程序因调用 BluetoothSocket.connect() 时出现 IO 异常而崩溃。我无法弄清楚发生了什么,所以如果有人可以帮助我,我将不胜感激。
我觉得可能和我随机生成的UUID有关系,但我不完全确定。
谢谢。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnScanDevice = (Button) findViewById( R.id.scandevice );
stateBluetooth = (TextView) findViewById( R.id.bluetoothstate );
startBluetooth();
listDevicesFound = (ListView) findViewById( R.id.devicesfound );
btArrayAdapter = new ArrayAdapter<String>( AndroidBluetooth.this,
android.R.layout.simple_list_item_1 );
listDevicesFound.setAdapter( btArrayAdapter );
CheckBlueToothState();
btnScanDevice.setOnClickListener( btnScanDeviceOnClickListener );
registerReceiver( ActionFoundReceiver, new IntentFilter( BluetoothDevice.ACTION_FOUND ) );
listDevicesFound.setOnItemClickListener( new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
myBtDevice = btDevicesFound.get( arg2 );
try {
btSocket = myBtDevice.createRfcommSocketToServiceRecord( MY_UUID );
iStream = btSocket.getInputStream();
oStream = btSocket.getOutputStream();
} catch ( IOException e ) {
Log.e( "Bluetooth Socket", "Bluetooth not available, or insufficient permissions" );
} catch ( NullPointerException e ) {
Log.e( "Bluetooth Socket", "Null Pointer One" );
}
myBtAdapter.cancelDiscovery();
CheckBlueToothState();
try {
btSocket.connect();
} catch ( IOException e ) {
Log.e( "Bluetooth Socket", "IO Exception" );
} catch ( NullPointerException e ) {
Log.e( "Bluetooth Socket", "Null Pointer Two" );
}
}
});
}
private void CheckBlueToothState() {
if( myBtAdapter == null ) {
stateBluetooth.setText("Bluetooth NOT supported" );
} else {
if( myBtAdapter.isEnabled() ) {
if( myBtAdapter.isDiscovering() ) {
stateBluetooth.setText( "Bluetooth is currently " +
"in device discovery process." );
} else {
stateBluetooth.setText( "Bluetooth is Enabled." );
btnScanDevice.setEnabled( true );
}
} else {
stateBluetooth.setText( "Bluetooth is NOT enabled" );
Intent enableBtIntent = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE );
startActivityForResult( enableBtIntent, REQUEST_ENABLE_BT );
}
}
}
private Button.OnClickListener btnScanDeviceOnClickListener = new Button.OnClickListener() {
public void onClick( View arg0 ) {
btArrayAdapter.clear();
myBtAdapter.startDiscovery();
}
};
@Override
protected void onActivityResult( int requestCode, int resultCode, Intent data ) {
if( requestCode == REQUEST_ENABLE_BT ) {
CheckBlueToothState();
}
}
private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver() {
public void onReceive( Context context, Intent intent ) {
String action = intent.getAction();
if( BluetoothDevice.ACTION_FOUND.equals( action ) ) {
BluetoothDevice btDevice = intent.getParcelableExtra( BluetoothDevice.EXTRA_DEVICE );
btDevicesFound.add( btDevice );
btArrayAdapter.add( btDevice.getName() + "\n" + btDevice.getAddress() );
btArrayAdapter.notifyDataSetChanged();
}
}
};
public static void startBluetooth(){
try {
myBtAdapter = BluetoothAdapter.getDefaultAdapter();
myBtAdapter.enable();
} catch ( NullPointerException ex ) {
Log.e( "Bluetooth", "Device not available" );
}
}
public static void stopBluetooth() {
myBtAdapter.disable();
}
最佳答案
您发布的代码有两个问题,但只有一个与您的崩溃有关。
很可能您在 logcat 中的崩溃显示类似“命令被拒绝”的内容。 UUID 是一个必须指向嵌入式设备上已发布服务的值,它不能随机生成。换句话说,您要访问的 RFCOMM SPP 连接具有一个特定的 UUID,它发布该 UUID 以标识该服务,并且当您创建套接字时,它必须使用匹配的 UUID。
This blog post I wrote可能会帮助您了解如何查询您的设备以获得需要插入到您的程序中的正确 UUID。在 Android 4.0 之前,SDK 几乎假定您提前知道它(您从蓝牙 OEM 等处获得它),因此从您的设备发现它有点迂回。如果您有幸拥有 4.0.3 设备,fetchUuidsWithSdp()
和 getUuids()
现在是公共(public)方法,您可以直接调用它们以查找所有已发布的服务和它们关联的 UUID 值。
你的代码的第二个问题可能会在以后遇到你,那就是在你连接之前你无法从你的套接字获取数据流,所以你可能需要像这样重写你的方法:
myBtDevice = btDevicesFound.get( arg2 );
try {
btSocket = myBtDevice.createRfcommSocketToServiceRecord( MY_UUID );
} catch ( IOException e ) {
Log.e( "Bluetooth Socket", "Bluetooth not available, or insufficient permissions" );
}
myBtAdapter.cancelDiscovery();
CheckBlueToothState();
try {
btSocket.connect();
//Get streams after connect() returns without error
iStream = btSocket.getInputStream();
oStream = btSocket.getOutputStream();
} catch ( IOException e ) {
Log.e( "Bluetooth Socket", "IO Exception" );
} catch ( NullPointerException e ) {
Log.e( "Bluetooth Socket", "Null Pointer Two" );
}
HTH
关于android - BluetoothSocket.connect() 抛出 IOException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10986285/
我有一个同时运行 3 个线程的应用程序。一个线程用于在手机和另一个蓝牙设备(Arduino)之间建立蓝牙连接。线程 2 通过蓝牙播放从另一部手机传入的音频。线程 3 录制音频并通过蓝牙将音频发送到另一
嗨我正在尝试实现一个蓝牙库,我想在其中连接一次 rfcomm 套接字,然后在所有调用中重用它。我想知道它是否已连接,以便知道我是否应该调用 connect方法。我在蓝牙套接字的源代码中找不到任何东西,
我是 Android 的新手。现在我正在尝试使用蓝牙 API 制作一个两人 Pong 游戏。我或多或少尝试过复制 Android 网站上的 BluetoothChat 教程,但在切换到 Connect
我们的设备通过蓝牙发送数据,在android应用中我们需要读取这些数据。 我能够建立蓝牙连接,接下来我调用线程以使用 BluetoothDevice 建立 BluetoothSocket 连接。此处读
我有一个应用程序,它通过蓝牙连接到 RaspberryPi,并在接收一些数据时将相同的数据循环给它。 我在连接时遇到了一些问题,因此需要使用此解决方法将我的 Android 手机连接到 Raspber
我已经创建了一个 Android 应用程序,如果我的 Android 有一个蓝牙连接到我的电脑并且我关闭了我的电脑,isConnected 仍然返回 true。有人知道这是否可以修复吗? 不知道有没有
我希望在我的 Android 应用程序中通过蓝牙执行设备到设备的文件传输。目前我已经编写了在多个蓝牙设备之间建立连接所需的代码,并且我已经检索到连接的 BluetoothSockets 以进行数据交换
我已经编写了一个用于连接外部配件的蓝牙 API。API 的设计方式是有一堆阻塞调用,例如 getTime、setTime、getVolume、setVolume等这些工作的方式是它们创建一个有效负载来
我以这种方式创建了 gpsSocket: final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
基本上,我一直在研究使用蓝牙或 wifi 的无线鼠标。我已经完成了所有工作,包括阅读和编写消息。但是,通过蓝牙传输数据的速度太慢,无法弥补。我到处都看了看,但我无法弄清楚是什么导致了这种速度。我正在使
我应该在蓝牙输入流的 readLine 上插入超时。 BluetoothDevice device = BluetoothAdapter.getDefaultAdapter()
我遇到了这种罕见的情况,在这种情况下,我尝试将 BluetoothSocket 连接到服务器,但连接方法不会返回。这是我的代码: device = _adapter.getRe
我正在编写一个将从另一台设备接收串行数据的应用程序(不是 Android,如果重要的话它将是 Bluesmirf Silver 模块,但现在我只在我的笔记本电脑蓝牙适配器上尝试)。这是我正在使用的代码
我的程序与充当服务器的嵌入式蓝牙设备连接。我成功找到了附近的蓝牙设备,但是当我尝试连接到新设备时,我的程序因调用 BluetoothSocket.connect() 时出现 IO 异常而崩溃。我无法弄
我无法启动 Java 服务器(在 Windows 7 x64 上使用 bluecove 2.1.1,外部蓝牙加密狗)和 Android 客户端(操作系统版本 2.3。 6). 设备发现工作正常,但我无
是否有机会为设置超时 BluetoothSocket.connect(); 以便该方法在一定时间后取消? 谢谢! 最佳答案 如果你想在设备在一定时间内没有连接时取消连接尝试,那么运行一个线程。 run
我使用修改后的Google Bluetooth Chat应用程序在两个 Android 设备(Android 5 和 Android 6)之间进行客户端-服务器蓝牙 RFCOMM 通信。 我的客户端应
我正在查看 Bluetooth Chat sample application来自 Google,他们在 UI 线程上写入 BluetoothSocket 的 OutputStream。那是对的吗?通
我正在尝试连接到配对的蓝牙 设备(baracoda d-fly 条形码阅读器)。我尝试了市场上的程序 GetBlueDemo,它成功地从它的套接字中读取。 我写了自己的概念证明,但当我尝试连接到设备时
连接到蓝牙后,我需要 child 通过那个套接字发送数据。但是如何将套接字转移到 childActivity? 最佳答案 不确定它是否是最好的设计,但我在这里所做的并且它在我的简单应用程序上工作是将蓝
我是一名优秀的程序员,十分优秀!