Java 在蓝牙模块上读取来自android的数据
这件事我已经坚持了好几天了。我已经想出了一个代码来发送字节的数据到我的蓝牙模块。我没有收到任何错误,但我不确定数据是否真的被发送了。我用示波器检查了模块上的RX和TX引脚,但什么也没有得到。有人知道怎么回事吗?这是我的密码:Java 在蓝牙模块上读取来自android的数据,java,android,eclipse,bluetooth,Java,Android,Eclipse,Bluetooth,这件事我已经坚持了好几天了。我已经想出了一个代码来发送字节的数据到我的蓝牙模块。我没有收到任何错误,但我不确定数据是否真的被发送了。我用示波器检查了模块上的RX和TX引脚,但什么也没有得到。有人知道怎么回事吗?这是我的密码: import java.io.IOException; import java.io.OutputStream; import java.util.Set; import java.util.UUID; import android.app.Activity; import
import java.io.IOException;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Modes extends Activity {
Button gaming;
Button graphics;
Button sound;
Button text;
BluetoothDevice mmDevice;
BluetoothSocket mmSocket;
OutputStream mmOutputStream;
BluetoothAdapter mBluetoothAdapter;
byte val;
private final UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.modes);
setUp();
gaming.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("gkghfgh");
val = 0;
try{
connect();
senData();
}catch(IOException ex){}
}
});
graphics.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
val = 1;
try{
connect();
senData();
}catch(IOException ex){}
}
});
sound.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
val = 8;
try{
connect();
senData();
}catch(IOException ex){}
}
});
text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
val = 9;
try{
connect();
senData();
}catch(IOException ex){}
startActivity(new Intent(Modes.this,Text.class));
}
});
}
private void setUp() {
gaming = (Button) findViewById(R.id.button1);
graphics = (Button) findViewById(R.id.button2);
sound = (Button) findViewById(R.id.button3);
text = (Button) findViewById(R.id.button4);
gaming.setText("Gaming");
graphics.setText("Graphics");
sound.setText("Sound");
text.setText("Text");
}
void connect() throws IOException{
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if(pairedDevices.size() > 0) {
for(BluetoothDevice device : pairedDevices) {
if(device.getName().equals("RN42-58C1")) {
mmDevice = device;
break;
}
}
}
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
mmSocket.connect();
}
void senData() throws IOException{
mmOutputStream.write(val);
System.out.println("Data Sent");
}
}
import java.io.IOException;
导入java.io.OutputStream;
导入java.util.Set;
导入java.util.UUID;
导入android.app.Activity;
导入android.bluetooth.BluetoothAdapter;
导入android.bluetooth.bluetooth设备;
导入android.bluetooth.BluetoothSocket;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
公共课堂模式扩展活动{
按钮游戏;
按钮图形;
按钮声;
按钮文本;
蓝牙设备;
蓝牙插座;
输出流mmOutputStream;
蓝牙适配器mBluetoothAdapter;
字节val;
专用最终UUID UUID=UUID.fromString(“000011101-0000-1000-8000-00805f9b34fb”);
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.modes);
设置();
gaming.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
System.out.println(“gkghfgh”);
val=0;
试一试{
connect();
senData();
}catch(IOException ex){}
}
});
graphics.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
val=1;
试一试{
connect();
senData();
}catch(IOException ex){}
}
});
sound.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
val=8;
试一试{
connect();
senData();
}catch(IOException ex){}
}
});
text.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
val=9;
试一试{
connect();
senData();
}catch(IOException ex){}
startActivity(新意图(Modes.this,Text.class));
}
});
}
私有无效设置(){
游戏=(按钮)findViewById(R.id.button1);
图形=(按钮)findViewById(R.id.button2);
声音=(按钮)findViewById(R.id.button3);
text=(按钮)findViewById(R.id.buttonn4);
博彩业。setText(“博彩业”);
graphics.setText(“图形”);
sound.setText(“sound”);
text.setText(“文本”);
}
void connect()引发IOException{
mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
设置pairedDevices=mBluetoothAdapter.getBondedDevices();
如果(pairedDevices.size()>0){
用于(蓝牙设备:pairedDevices){
if(device.getName()等于(“RN42-58C1”)){
mmDevice=设备;
打破
}
}
}
mmSocket=mmDevice.createRfcommSocketToServiceRecord(uuid);
mmSocket.connect();
}
void senData()引发IOException{
mmOutputStream.write(val);
System.out.println(“发送的数据”);
}
}
我认为问题在于您的蓝牙设备名称。您需要写入蓝牙ID,它是一个12位数字,具有字母数字值
希望能奏效。祝你好运。尝试在“mmOutputStream.write(val);”之后调用“mmOutputStream.flush();”