Java android蓝牙中的发送字符串

Java android蓝牙中的发送字符串,java,android,import,bluetooth,Java,Android,Import,Bluetooth,我的主要项目是基于android的家庭自动化系统,这意味着我必须使用android蓝牙控制家用电器。我使用的是HC-05蓝牙模块,它接收通过我的应用程序发送给它的字符串,将其发送给PIC16f877a,pic根据接收到的字符串依次控制设备。现在我的问题是,我已经能够通过我的应用程序配对和连接设备,但我的应用程序没有发送我想在单击按钮时发送的字符。请注意,PIC和蓝牙模块工作正常。请帮帮我。我把我的代码贴在下面,希望你们不要介意 package com.chainedcat.splashscree

我的主要项目是基于android的家庭自动化系统,这意味着我必须使用android蓝牙控制家用电器。我使用的是HC-05蓝牙模块,它接收通过我的应用程序发送给它的字符串,将其发送给PIC16f877a,pic根据接收到的字符串依次控制设备。现在我的问题是,我已经能够通过我的应用程序配对和连接设备,但我的应用程序没有发送我想在单击按钮时发送的字符。请注意,PIC和蓝牙模块工作正常。请帮帮我。我把我的代码贴在下面,希望你们不要介意

package com.chainedcat.splashscreen;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.UUID;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivityActivity extends Activity{

private BluetoothAdapter BA;
private ArrayList<BluetoothDevice> devices;
private ListView lv;
private TextView text;
private BluetoothSocket btSocket=null;
private ArrayAdapter<String> btArrayAdapter;
private static final int REQUEST_ENABLE_BT = 1;
private UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34EB");
private OutputStream outStream=null;

public void Init(){
    BA = BluetoothAdapter.getDefaultAdapter();
    text =(TextView) findViewById(R.id.text);
    lv = (ListView) findViewById(R.id.listView1);
    devices = new ArrayList<BluetoothDevice>();
    btArrayAdapter= new ArrayAdapter<String>this,android.R.layout.simple_list_item_1); 
    lv.setAdapter(btArrayAdapter);
}

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    Init();
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
        BluetoothDevice selectedDevice = devices.get(arg2);
           if(selectedDevice.getBondState()== BluetoothDevice.BOND_NONE){
                 pairDevice(selectedDevice);
            }
           else if(selectedDevice.getBondState()==BluetoothDevice.BOND_BONDED){
                connect(selectedDevice);
            }
        }
    });
}

public void bluetooth(View view){
    if(!BA.isEnabled()){
    Intent btIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(btIntent, REQUEST_ENABLE_BT);
    Toast.makeText(getApplicationContext(), "Bluetooth turned on!!", Toast.LENGTH_LONG).show();
        btExtra();
    }else{
    Toast.makeText(getApplicationContext(), "Bluetooth is already on!!", Toast.LENGTH_LONG).show();
    btExtra();
    }

}

public void btExtra(){

    btArrayAdapter.clear();
    BA.startDiscovery();
    registerReceiver(btReceiver, new tentFilter(BluetoothDevice.ACTION_FOUND));
    }

final BroadcastReceiver btReceiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if(BluetoothDevice.ACTION_FOUND.equals(action)){
            BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            devices.add(device);
            String status=null;
            if(device.getBondState()==BluetoothDevice.BOND_BONDED){
                status="paired";
            }else{status="not paired";}
            btArrayAdapter.add(device.getName()+" : "+status+"\n"+device.getAddress());
            btArrayAdapter.notifyDataSetChanged();
        }
    }
};

@Override
protected void onActivityResult(int requestCode,int resultCode, Intent data){
    if(requestCode==REQUEST_ENABLE_BT){
        if(BA.isEnabled()){
            text.setText("Bluetooth Status:Enabled");
        }else{
            text.setText("Bluetooth Status:Disabled");
        }           
    }
}

private void pairDevice(BluetoothDevice device) {
     Method m=null;
    try {
        m = device.getClass().getMethod("createBond", (Class[]) null);
    } catch (NoSuchMethodException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     try {
        m.invoke(device, (Object[]) null);
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     connect(device);
}

public void connect(BluetoothDevice device){
    Toast.makeText(getApplicationContext(), "connecting....", Toast.LENGTH_LONG).show();
    try {
        btSocket=device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    BA.cancelDiscovery();
    try {
        Toast.makeText(getApplicationContext(), "entered in run", Toast.LENGTH_LONG).show();
        btSocket.connect();
        if(btSocket!=null){btSocket.close();}
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void send(String s){
    try {
        outStream = btSocket.getOutputStream();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    byte[] bytes = s.getBytes();
    try {
        outStream.write(bytes);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void fan(View v){
    send("f");
}

@Override
public void onDestroy(){
    super.onDestroy();
    BA.disable();
    unregisterReceiver(btReceiver);
}
}
package com.chainedcat.splashscreen;
导入java.io.IOException;
导入java.io.OutputStream;
导入java.lang.reflect.InvocationTargetException;
导入java.lang.reflect.Method;
导入java.util.ArrayList;
导入java.util.UUID;
导入android.app.Activity;
导入android.bluetooth.BluetoothAdapter;
导入android.bluetooth.bluetooth设备;
导入android.bluetooth.BluetoothSocket;
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.content.IntentFilter;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.AdapterView.OnItemClickListener;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
导入android.widget.TextView;
导入android.widget.Toast;
公共类MainActivityActivity扩展活动{
私人蓝牙适配器BA;
专用阵列列表设备;
私有ListView lv;
私有文本查看文本;
私有BluetoothSocket btSocket=null;
专用阵列适配器btArrayAdapter;
私有静态最终整数请求_ENABLE_BT=1;
私有UUID MY_UUID=UUID.fromString(“00001101-0000-1000-8000-00805F9B34EB”);
私有OutputStream outStream=null;
公共void Init(){
BA=BluetoothAdapter.getDefaultAdapter();
text=(TextView)findViewById(R.id.text);
lv=(ListView)findViewById(R.id.listView1);
设备=新的ArrayList();
btArrayAdapter=newarrayadapter这个,android.R.layout.simple\u list\u item\u 1);
低压设置适配器(btArrayAdapter);
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main_活动);
Init();
lv.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共链接(AdapterView arg0、视图arg1、内部arg2、,
长arg3){
//TODO自动生成的方法存根
BluetoothDeviceSelectedDevice=devices.get(arg2);
if(selectedDevice.getBondState()==BluetoothDevice.BOND\u无){
配对设备(所选设备);
}
else if(selectedDevice.getBondState()==BluetoothDevice.BOND\u BONDED){
连接(所选设备);
}
}
});
}
公共无效蓝牙(视图){
如果(!BA.isEnabled()){
意向btIntent=新意向(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
startActivityForResult(btIntent、REQUEST\u ENABLE\u BT);
Toast.makeText(getApplicationContext(),“蓝牙已打开!!”,Toast.LENGTH\u LONG.show();
btExtra();
}否则{
Toast.makeText(getApplicationContext(),“蓝牙已经开启!!”,Toast.LENGTH\u LONG.show();
btExtra();
}
}
公众假期额外费用(){
btArrayAdapter.clear();
BA.startDiscovery();
registerReceiver(btReceiver,新的tentFilter(BluetoothDevice.ACTION_FOUND));
}
最终BroadcastReceiver btReceiver=新的BroadcastReceiver(){
@凌驾
公共void onReceive(上下文、意图){
String action=intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(ACTION)){
BluetoothDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA\u设备);
设备。添加(设备);
字符串状态=空;
if(device.getBondState()==BluetoothDevice.BOND\u BOND){
status=“paired”;
}else{status=“not paired”;}
btArrayAdapter.add(device.getName()+“:“+status+”\n“+device.getAddress());
btArrayAdapter.notifyDataSetChanged();
}
}
};
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
if(requestCode==请求启用){
if(BA.isEnabled()){
text.setText(“蓝牙状态:已启用”);
}否则{
text.setText(“蓝牙状态:禁用”);
}           
}
}
专用无效配对设备(蓝牙设备){
方法m=null;
试一试{
m=device.getClass().getMethod(“createBond”,(Class[])null;
}捕获(无此方法例外){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
试一试{
m、 调用(设备,(对象[])null);
}捕获(非法访问例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IllegalArgumentException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(调用TargetException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
连接(设备);
}
公共void connect(蓝牙设备){
Toast.makeText(getApplicationContext(),“connecting…”,Toast.LENGTH_LONG.show();
试一试{
btSocket=device.createInsurerCommsocketToServiceRecord(我的UUID);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
BA.cancelDiscovery();
试一试{
Toast.makeText(getApplicationContext(),“在运行中输入”,Toast.LENGTH_LONG.show();
btSocket.connect();
如果(btSocket!=null){btSocket.close();}
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
公共无效发送(字符串s){
试一试{
outStream=btSocket.getOutputStream();
}捕获(IOE异常)