2 f823154 f823154 于 2016.04.10 17:01 提问

Android蓝牙开发,与HC-06单片机通信,

单片机发,android端收,android端代码怎么搜索蓝牙设备并连接成功呢?

3个回答

qq_33232071
qq_33232071   2016.04.11 08:25

最简单的方法:
先到android store上下载一个app做测试,比如‘spp蓝牙‘这个APP
上面有搜索,连接的步骤
你的源码来仿照它的写

CSDNXIAOD
CSDNXIAOD   2016.04.10 17:12

android 与 蓝牙模块 hc06通信app 开发要点
安卓手机通过蓝牙与单片机通信
Android 通信--蓝牙
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

hanxinxin_com
hanxinxin_com   2016.04.18 10:06
 package kankan.idea.bluetoolswitch;


import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;

public class Bluetoolswitch extends Activity implements OnClickListener
{

    ImageButton kaiguan1 ;
    ImageButton kaiguan2 ;
    Button lianjielanya;
    int kaiguan1flag = 0;
    int kaiguan2flag = 0;
    BluetoothAdapter btadapter;

    List<String> devicesfound = new ArrayList<String>();
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bluetoolswitch);
        kaiguan1 = (ImageButton)findViewById(R.id.switch11);
        kaiguan2 = (ImageButton)findViewById(R.id.switch22);
        lianjielanya = (Button)findViewById(R.id.connectbluetools);
        kaiguan1.setOnClickListener(Bluetoolswitch.this);
        kaiguan2.setOnClickListener(Bluetoolswitch.this);
        lianjielanya.setOnClickListener(Bluetoolswitch.this);


        // 注册Receiver来获取蓝牙设备相关的结果
        IntentFilter intent = new IntentFilter();
        intent.addAction(BluetoothDevice.ACTION_FOUND);// 用BroadcastReceiver来取得搜索结果
        intent.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        intent.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
        intent.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        registerReceiver(searchdevices, intent);

    }

    boolean openbluetools()
    {
        btadapter = BluetoothAdapter.getDefaultAdapter();// 初始化本机蓝牙功能
        if(null == btadapter)
        {
            return false;//没有蓝牙设备
        }

        if((BluetoothAdapter.STATE_OFF == btadapter.getState()) || 
           (BluetoothAdapter.STATE_TURNING_OFF == btadapter.getState()) ||
           (BluetoothAdapter.STATE_TURNING_ON == btadapter.getState()))
        {
            if(btadapter.enable())
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return true;
        }
    }


    BroadcastReceiver searchdevices = new BroadcastReceiver() 
    {
        public void onReceive(Context context, Intent intent) 
        {
            String action = intent.getAction();
            Bundle b = intent.getExtras();
            Object[] lstName = b.keySet().toArray();
            // 显示所有收到的消息及其细节
            for (int i = 0; i < lstName.length; i++) 
            {
                String keyName = lstName[i].toString();
                Log.v("test2",keyName +":"+ String.valueOf(b.get(keyName)));
            }
            //搜索设备时,取得设备的MAC地址
            if (BluetoothDevice.ACTION_FOUND.equals(action)) 
            {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                String str= device.getName() + "|" + device.getAddress();
                if (-1 == devicesfound.indexOf(str))// 防止重复添加
                    devicesfound.add(str); // 获取设备名称和mac地址
            }
        }
    };



    @Override
    public void onClick(View v) 
    {
        // TODO Auto-generated method stub
        switch(v.getId())
        {
            case R.id.connectbluetools:
            {
                if(true == openbluetools())
                {
                    Log.v("test","打开设备成功"+"本机蓝牙地址:" + btadapter.getAddress());

                }
                else
                {
                    Log.v("test","打开设备失败");
                }
            }break;
            case R.id.switch11:
            {
                if(0 == kaiguan1flag)
                {
                    Log.v("test","一路开");
                    kaiguan1flag = 1;
                    kaiguan1.setImageDrawable(getResources().getDrawable(R.drawable.switchon1));    
                }
                else
                {
                    Log.v("test","一路关");
                    System.out.print("asdfasdfsafas");
                    kaiguan1flag = 0;
                    kaiguan1.setImageDrawable(getResources().getDrawable(R.drawable.switchoff1));   
                }
            }break;
            case R.id.switch22:
            {
                if(0 == kaiguan2flag)
                {
                    Log.v("test","二路开");
                    kaiguan2flag = 1;
                    kaiguan2.setImageDrawable(getResources().getDrawable(R.drawable.switchon1));    
                }
                else
                {
                    Log.v("test","二路关");
                    kaiguan2flag = 0;
                    kaiguan2.setImageDrawable(getResources().getDrawable(R.drawable.switchoff1));   
                }

            }break;
            default:
            {

            }break;
        }
    }



}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!