首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >蓝牙IOException :蓝牙已关闭

蓝牙IOException :蓝牙已关闭
EN

Stack Overflow用户
提问于 2017-02-12 19:58:02
回答 1查看 100关注 0票数 0

我试着在Android智能手机和Mindstorms NXT之间建立蓝牙连接。这款智能手机是搭载Android 5.1.1的索尼Xperia Zl。应用程序应向NXT发送一个号码,NXT应通过运行的程序接收该号码。这是我当前的源代码:

代码语言:javascript
复制
package com.mona.projektkurs;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.UUID;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;


public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    enableBt();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}



 private BluetoothAdapter adapter;
   private BluetoothDevice device = null;
   private BluetoothSocket BTsocket=null;

public void enableBt(){
    adapter = BluetoothAdapter.getDefaultAdapter();
    if(!adapter.isEnabled()){
        adapter.enable();
        connection();
    }else{
        Toast.makeText(getApplicationContext(),"Error1" , Toast.LENGTH_LONG).show();
    }
}

public void connection(){
    device = adapter.getRemoteDevice("00:16:53:1B:1D:C5");
    try{
         BTsocket = device.createRfcommSocketToServiceRecord(UUID
                  .fromString("00001101-0000-1000-8000-00805F9B34FB"));
         BTsocket.connect();
         messageWrite();
    }catch(IOException e){
        Toast.makeText(getApplicationContext(), "Error2"+e.toString(), Toast.LENGTH_LONG).show();
    }
}

public void messageWrite(){
    if(BTsocket != null){
        try{
             OutputStreamWriter out = new OutputStreamWriter(BTsocket.getOutputStream());
             out.write((byte)0x80); // no respond
             out.write((byte)0x09); // MessageWrite
             out.write((byte)0x03); // Inbox (0-9)
             out.write((byte)0x02); // MessageSize
             out.write((byte)0x49); // Data ("1" == 0x49 ASCII Code)
             out.write((byte)0x00); // Null termination (/0)
             BTsocket.close();
        }catch(IOException f){
            Toast.makeText(getApplicationContext(), "Error3"+f.toString(), Toast.LENGTH_LONG).show();


    }
        }
    }

}

蓝牙已成功启用,但错误处理程序抛出IO.Exception:蓝牙已关闭。MAC-Adress已验证。可以使用商用App成功建立蓝牙连接。

有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2017-02-12 20:20:10

你在清单中添加蓝牙权限了吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42187276

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档