服务发现在Android上使用蓝牙失败的例外(Service discovery failed exception using Bluetooth on Android)
我目前正在使用通过蓝牙连接到仪器的Android应用程序,需要编写字符串命令并接收字符串响应。 目前我已经通过Wi-Fi连接/读/写工作在TCP / IP,现在试图实现蓝牙。 但我遇到了一些障碍。 我一直在搜索网页,试图找到类似的东西,没有任何运气。 我一直在使用Android开发人员资源的例子:蓝牙聊天作为我的主要参考点。
我当前的代码似乎工作..然后在连接点抛出一个服务发现失败的异常。 我正在使用
DeviceListActivity
类来进行发现和选择要连接的设备。 它返回anActivityResult,然后我的蓝牙类等待它来处理它,然后连接到它。 下面的代码与蓝牙聊天应用程序几乎相同。public void onActivityResult(int requestCode, int resultCode, Intent data) { if(!m_BluetoothAdapter.isEnabled()) { m_BluetoothAdapter.enable(); } switch (requestCode) { case REQUEST_CONNECT_DEVICE: // When DeviceListActivity returns with a device to connect if (resultCode == Activity.RESULT_OK) { // Get the device MAC address String address = data.getExtras() .getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS); // Get the BLuetoothDevice object BluetoothDevice device = m_BluetoothAdapter.getRemoteDevice(address); // Attempt to connect to the device connect(device); } break; case REQUEST_ENABLE_BT: // When the request to enable Bluetooth returns if (resultCode == Activity.RESULT_OK) { // Bluetooth is now enabled, so set up a chat session } else { // User did not enable Bluetooth or an error occured Toast.makeText(this, "Bluetooth not enabled", Toast.LENGTH_SHORT).show(); finish(); } } }
这是我的连接功能:
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); private void connect(BluetoothDevice device) { m_Device = device; BluetoothSocket tmp = null; // Get a BluetoothSocket for a connection with the // given BluetoothDevice try { tmp = device.createRfcommSocketToServiceRecord(MY_UUID); } catch (IOException e) { } m_Socket = tmp; m_BluetoothAdapter.cancelDiscovery(); try { // This is a blocking call and will only return on a // successful connection or an exception m_Socket.connect(); } catch (IOException e) { try { m_Socket.close(); } catch (IOException e2) { } return; } }
希望无论我做错了什么都很简单,但是恐怕这并不容易。 这是我第一次做任何蓝牙开发,也许我正在做一些公然错误的事情...但我不知道为什么我得到服务发现失败的例外。
您可以随时手动配对/查找设备...它需要密码,但我不认为这是我遇到的问题。
I'm currently working on an Android application that connects to an instrument via Bluetooth and need to write string commands and receive string responses back. Currently I have the connect/read/write working for TCP/IP over Wi-Fi and now trying to implement Bluetooth. But I am running into some roadblocks. I have been searching the web trying to find examples of something similar and haven't had any luck. I have been using the Android developer resource example: Bluetooth Chat as my main reference point.
My current code seems to work.. Then it throws a Service Discovery Failed exception at the point of the connection. I am using the
DeviceListActivity
class to do the discovery and selecting of the device I want to connect to. It returns anActivityResult and then my Bluetooth class waits for it to handle that and then does the connect to it. The code beneath is almost identical to the Bluetooth Chat App.public void onActivityResult(int requestCode, int resultCode, Intent data) { if(!m_BluetoothAdapter.isEnabled()) { m_BluetoothAdapter.enable(); } switch (requestCode) { case REQUEST_CONNECT_DEVICE: // When DeviceListActivity returns with a device to connect if (resultCode == Activity.RESULT_OK) { // Get the device MAC address String address = data.getExtras() .getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS); // Get the BLuetoothDevice object BluetoothDevice device = m_BluetoothAdapter.getRemoteDevice(address); // Attempt to connect to the device connect(device); } break; case REQUEST_ENABLE_BT: // When the request to enable Bluetooth returns if (resultCode == Activity.RESULT_OK) { // Bluetooth is now enabled, so set up a chat session } else { // User did not enable Bluetooth or an error occured Toast.makeText(this, "Bluetooth not enabled", Toast.LENGTH_SHORT).show(); finish(); } } }
This is my connect function:
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); private void connect(BluetoothDevice device) { m_Device = device; BluetoothSocket tmp = null; // Get a BluetoothSocket for a connection with the // given BluetoothDevice try { tmp = device.createRfcommSocketToServiceRecord(MY_UUID); } catch (IOException e) { } m_Socket = tmp; m_BluetoothAdapter.cancelDiscovery(); try { // This is a blocking call and will only return on a // successful connection or an exception m_Socket.connect(); } catch (IOException e) { try { m_Socket.close(); } catch (IOException e2) { } return; } }
Hopefully, whatever I am doing wrong is simple, but I'm afraid it's never that easy. This is my first time doing any Bluetooth development, and maybe I'm doing something blatantly wrong... But I'm not sure why I get the service discovery failed exception.
You can pair/find the device at all times manually on the phone... It does require a passcode, but I don't think that is the problem that I am having.
原文:https://stackoverflow.com/questions/3397071
最满意答案
这是因为
closest
只向上搜索DOM。 换句话说,closest
开始按父元素搜索,然后是祖父元素等。你应该使用
siblings
。$(this).siblings('input.finishId').val();
It's because
closest
only searches DOM upward. In other words,closest
starts searching by father element, then grandfather element, etc.You should use
siblings
instead.$(this).siblings('input.finishId').val();
相关问答
更多-
参考Ani补充的链接,在这种情况下,它会是这样的: ret = source.SkipWhile(pair => pair.Key <= start).First().Value; 或者可能(允许Try式使用) using(var iter = source.GetEnumerator()) { while(iter.MoveNext()) { if(iter.Current.Key > start) { ret = iter.Current.Value; ...
-
它只能使用css完成 在这个片段中,它将选中一个复选框,然后检查它的同级类并设置可见性 input[type='checkbox']:checked ~ .hidden-textbox{ visibility:visible; } .hidden-textbox { visibility: hidden; } input[type='checkbox']:checked~.hidden-textbox { visibility: visible; }您需要包含一些逻辑来检查当前年份的位置: var d = new Date(); var y = d.getFullYear(); if (d.getMonth() == 11 && d.getDate() > 25) y += 1; $('#event_date').val("12/25/" + y); You need to include some logic to check where you are in the current year: var d = new Date(); var ...
jquery获取
中的下一个输入元素值(jquery get next input element value in )[2022-04-23] 尝试 $('.myclass').on('keyup', function () { var disc = $(this).closest('td').next().find('input').val(); }); 要么 $('.myclass').on('keyup', function () { var disc = $('.anotherclass').val(); }); Try $('.myclass').on('keyup', function () { var di ...这适用于gcc 4.9.3: #include#include #include int main(void) { int i, j, k, end; char podatak[100]; int *sifre = malloc(100 * sizeof(int)); int *faktori = malloc(100 * sizeof(int)); j = k = 0; do ... 原型 - 在HTMLInputElement之后获取下一个文本输入(Prototype - get next text input after an HTMLInputElement)[2023-09-23]
您可以使用Element.next和CSS规则: $('element').next('input[type=text]'); You can use Element.next with a CSS rule: $('element').next('input[type=text]');首先,您必须使用closest()到达父级ul ,然后分别使用next()或prev()转到下一个或上一个ul并使用find()在后代中查找输入 现场演示 $("a").click(function() { $(this).closest('ul').prev('ul').find(":input"); $(this).closest('ul').next('ul').find(":input"); }); First you have to reach the parent ul u ...使用jQuery UI自动完成更改下一个输入元素值(Changing the next input element value using jQuery UI Autocomplete)[2022-09-23]
我原来的回答是: $(event.target).parent().next() 那是因为.next()不会超出当前的嵌套级别。 你必须回到td,然后得到下一个孩子(td的父母,这是tr)。 然而... 更好的答案:不要将输入元素直接放在tr元素中。 这是无效的。 这是一个例子,我用自动完成功能移动了td中的隐藏元素,并将其更改为键入文本(将其更改为隐藏以隐藏它): var autoCompVals = { minLength: 0, source: function (request ...allnames1.add(“a”) allnames1.add(“b”)就足够了。 如果它不起作用; 我认为这是因为allnames1每次都是新创建的..或者sc.nextline是错误的。 allnames1.add ("a") allnames1.add ("b") is enough. If it doesn't work; I think its because allnames1 is newly created every time.. or sc.nextline is wrong.您需要通过字符串连接添加显示的现有值加上新的值: function addToDisplay(x) { $('#display').val($('#display').val() + x); if (x === 'c') { $('#display').val(''); } }; You need to add the existing value of the display plus the new one via string concatenation: function a ...相关文章
更多- Hive 终端产生的问题 (Failed to start database 'metastore_db', see the next exception for details.)
- [Hadoop Err] Call to ? failed on local exception: java.net.NoRouteToHostException: No route to host
- Windows Moible, Wince 使用.NET Compact Framework的进行蓝牙(Bluetooth)开发 之 蓝牙虚拟串口 (Bluetooth Virtual Serial Port)
- android Failed to install .apk on device 'emulator-5554': timeout的解决方案
- 移动MM failed to find resource file{mmiap.xml}
- Nutch Job failed异常
- 崩溃了,android 创建表失败。。 大家看看吧。。。
- android---TextView赋值问题
- android布局问题
- solr bin/snapinstaller 报 failed to connect to Solr server
最新问答
更多- 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
- 如何通过引用返回对象?(How is returning an object by reference possible?)
- 矩阵如何存储在内存中?(How are matrices stored in memory?)
- 每个请求的Java新会话?(Java New Session For Each Request?)
- css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
- 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
- xcode语法颜色编码解释?(xcode syntax color coding explained?)
- 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
- 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
- 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
- 西安哪有PLC可控制编程的培训
- 在Entity Framework中选择基类(Select base class in Entity Framework)
- 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
- 电脑二级VF有什么用
- Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
- 金华英语角.
- 手机软件如何制作
- 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
- 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
- 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
- Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
- 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
- python的访问器方法有哪些
- DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
- 在Ruby中对组合进行排序(Sorting a combination in Ruby)
- 网站开发的流程?
- 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
- 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
- 透明度错误IE11(Transparency bug IE11)
- linux的基本操作命令。。。