我正在编写一个需要与蓝牙 2.1 设备交换数据的应用程序。 我已经做过好几次了,但这次发生了一些奇怪的事情。
Log.d("TAG", "connectToDevice");
if(macAddress != null)
deviceToConnect = mBluetoothAdapter.getRemoteDevice(macAddress);
Log.d("TAG", "macAddress != null");
if(deviceToConnect != null)
try {
btSocket = deviceToConnect.createRfcommSocketToServiceRecord(UUID.fromString(SharedIncludes.SPP_UUID));
} catch (IOException e) {
btSocket = null;
e.printStackTrace();
}
Log.d("TAG", "deviceToConnect != null");
if(btSocket != null){
try {
inputStream = btSocket.getInputStream();
Log.d("TAG", "inputStream OK");
} catch (IOException e) {
inputStream = null;
Log.d("TAG", "inputStream KO");
e.printStackTrace();
}
try {
outputStream = btSocket.getOutputStream();
Log.d("TAG", "outputStream OK");
} catch (IOException e) {
outputStream = null;
Log.d("TAG", "outputStream KO");
e.printStackTrace();
}
}
Log.d("TAG", "btSocket != null");
Log.d("TAG", "onConnectionEstablished");
在发现阶段之后,我得到了我需要连接的 BluetoothDevice,然后我获得了套接字、输入和输出流。
然后我有一个从输入流中读取的线程。
int byteRead;
while (listening) {
try {
if(inputStream!=null){
Log.d("TAG", "inputStream: " +inputStream);
byteRead = inputStream.read();
}else{
Log.d("TAG", "inputStream is null!");
continue;
} // Rest of the code here
我在执行 inputStream.read() 行时遇到此错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.io.InputStream.read(byte[], int, int)' on a null object reference
at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:427)
at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:60)
at com.me.testapplication.Connection_BT21.run(Connection_BT21.java:152)
问题 1:如果我正在检查 inputStream != null,为什么会出现 NullPointerException?
问题 2:如果我尝试调用 read(),为什么要 read(byte[], int, int)?
最佳答案
我发现了错误,你没问题:套接字有错误。我需要调用 socket.connect()!
if(macAddress != null)
deviceToConnect = mBluetoothAdapter.getRemoteDevice(macAddress);
Log.d("TAG", "macAddress != null");
if(deviceToConnect != null)
try {
btSocket = deviceToConnect.createRfcommSocketToServiceRecord(UUID.fromString(SharedIncludes.SPP_UUID));
} catch (IOException e) {
btSocket = null;
e.printStackTrace();
}
Log.d("TAG", "deviceToConnect != null");
if(btSocket != null){
//This was the line missing!
btSocket.connect();
try {
inputStream = btSocket.getInputStream();
Log.d("TAG", "inputStream OK");
} catch (IOException e) {
inputStream = null;
Log.d("TAG", "inputStream KO");
e.printStackTrace();
}
try {
outputStream = btSocket.getOutputStream();
Log.d("TAG", "outputStream OK");
} catch (IOException e) {
outputStream = null;
Log.d("TAG", "outputStream KO");
e.printStackTrace();
}
}
这就是为什么 inputStream 没有准备好,我得到了那个错误。
抱歉,我只是没有看到它..希望它可以帮助别人!
关于android - inputStream.read() 导致 NullPointerException(检查 inputStream 后!=null),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24267671/
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife
我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)
我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案
CSV.open(name,"r").eachdo|row|putsrowend我得到以下错误:CSV::MalformedCSVErrorUnquotedfieldsdonotallow\ror\n文件名是一个.txt制表符分隔文件。我是专门做的。我有一个.csv文件,我转到excel,并将文件保存为.txt制表符分隔的文件。所以它是制表符分隔的。CSV.open不应该能够读取制表符分隔的文件吗? 最佳答案 尝试像这样指定字段分隔符:CSV.open("name","r",{:col_sep=>"\t"}).eachdo|row|
我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查
我的日期格式如下:"%d-%m-%Y"(例如,今天的日期为07-09-2015),我想看看是不是在过去的七天内。谁能推荐一种方法? 最佳答案 你可以这样做:require"date"Date.today-7 关于ruby-检查日期是否在过去7天内,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/32438063/
如何检查Ruby文件是否是通过“require”或“load”导入的,而不是简单地从命令行执行的?例如:foo.rb的内容:puts"Hello"bar.rb的内容require'foo'输出:$./foo.rbHello$./bar.rbHello基本上,我想调用bar.rb以不执行puts调用。 最佳答案 将foo.rb改为:if__FILE__==$0puts"Hello"end检查__FILE__-当前ruby文件的名称-与$0-正在运行的脚本的名称。 关于ruby-检查是否
我有一个div,它根据表单是否正确提交而改变。我想知道是否可以检查类的特定元素?开始元素看起来像这样。如果输入不正确,添加错误类。 最佳答案 试试这个:browser.div(:id=>"myerrortest").class_name更多信息:http://watir.github.com/watir-webdriver/doc/Watir/HTMLElement.html#class_name-instance_method另一种选择是只查看具有您期望的类的div是否存在browser.div((:id=>"myerrortes
我的模型有defself.empty_building//stuffend我怎样才能对这个现有的进行rspec?,已经尝试过:describe"empty_building"dosubject{Building.new}it{shouldrespond_to:empty_building}endbutgetting:Failure/Error:it{shouldrespond_to:empty_building}expected#torespondto:empty_building 最佳答案 你有一个类方法self.empty_bu