安卓蓝牙入门二-------整合版本

本人目前刚接触蓝牙不久,由于自己摸索走了很多弯路,现在摸索出来了一点东西,希望写下来对大家,特别是新手有点帮助

废话不多说直接上代码:


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <Button
        android:id="@+id/on"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="on"
        android:text="Turn On" />


    <Button
        android:id="@+id/visible"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="visible"
        android:text="Get Visible" />


    <Button
        android:id="@+id/getband"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="getBand"
        android:text="List getBand" />


    <Button
        android:id="@+id/off"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="off"
        android:text="Turn Off" />


    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        
        android:background="#9999FF">
    </ListView>
    <TextView
        android:id="@+id/Tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text=""
        />

</LinearLayout>


package com.example.blus;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;


import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity implements android.content.DialogInterface.OnClickListener {
private TextView TV;
private ListView LV;
private BluetoothDevice BD;
private Set<BluetoothDevice>pairedDevices;
private BluetoothAdapter adapter;
private BluetoothDevice device;
private ArrayList<String> list=new ArrayList<String>();
private ArrayAdapter<String> ad=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xx);
        
        inift();     
        
        adapter=BluetoothAdapter.getDefaultAdapter();//获得本地蓝牙适配器
    }
public void inift() {
TV=(TextView)findViewById(R.id.Tv);
LV=(ListView)findViewById(R.id.listView1);

开启蓝牙设备//
public void on(View view){
setTitle("本机蓝牙地址:" + adapter.getAddress());  
if (!adapter.isEnabled()) {
        Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(turnOn, 0);
        Toast.makeText(MainActivity.this,"Turned on" 
        ,Toast.LENGTH_LONG).show();
}else{Toast.makeText(MainActivity.this,"Already on",
        Toast.LENGTH_LONG).show();}
}
//得到绑定的蓝牙设备
public void getBand(View view){
bd();
if(!adapter.isEnabled()){Toast.makeText(getApplicationContext(), "蓝牙设备没有开启", 6).show();}
else{
Dialog dialog = new AlertDialog.Builder(this)
.setTitle("已经绑定的蓝牙设备")
.setIcon(R.drawable.ic_launcher)
.setMessage(TV.getText())
.setPositiveButton("确定", this)
.setNeutralButton("退出",this)
 .create();        
dialog.show();
TV.setText("");
}       
}
public void bd(){
pairedDevices = adapter.getBondedDevices();
    for(BluetoothDevice bt : pairedDevices)
    TV.setText(bt.getName()+"  :  "+bt.getAddress()+"\n");
}
/


/蓝牙收索//
public class  BluetoothReceiver extends BroadcastReceiver{


@Override
public void onReceive(Context arg0, Intent intent) {
// TODO Auto-generated method stub
String str=null;
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// TV.setText(TV.getText()+device.getName()+"  :  "+device.getAddress()+"\n");
// list.add(device.getName()+"  :  "+device.getAddress());
if (device.getBondState() == BluetoothDevice.BOND_BONDED) {  
              str ="已配对|"+ device.getName() + "|"  
                     + device.getAddress();  
              
         } 
if(device.getBondState() == BluetoothDevice.BOND_NONE){
        str ="未配对|"+ device.getName() + "|"  
                     + device.getAddress();  
         } 
if(list.indexOf(str)==-1){//防止出现相同的
        list.add(str);
         }
ad=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_expandable_list_item_1,list);
LV.setAdapter(ad); 
}
}
public void visible(View view){
//1.注册一个广播,用于接收“发现设备”的广播
//.list.removeAll(list);
list.clear();
if(adapter.isEnabled()){
IntentFilter intentFilter =new IntentFilter(device.ACTION_FOUND);
     BluetoothReceiver receiver = new BluetoothReceiver();
     registerReceiver(receiver, intentFilter);
     adapter.startDiscovery();
     Toast.makeText(getApplicationContext(), "开始收索蓝牙设备", 6).show();
}else{Toast.makeText(getApplicationContext(), "蓝牙设备没有打开", 6).show();}
//BluetoothReceiver receiver = new BluetoothReceiver();
   //  if(adapter.isEnabled()){
    // }
}



/蓝牙关闭/
public void off(View view){
LV.setAdapter(null);
list.clear();
if(adapter.isEnabled()){
adapter.disable();
Toast.makeText(getApplicationContext(), "蓝牙关闭中。。", 6).show();
TV.setText("");
}
}

@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub

}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ble audio补丁原理是利用hidraw节点捕捉协议栈发送的语音数据,目前Android Blueroid将ble语音数据和按键信息通过hid发送出去,通过建立hidraw节点,可以从中捕捉到语音数据。目前通过ble hal实现从hidraw中读取遥控器语音数据,在Android框架层上就通过配置文件将ble hal导入到音频框架中,并通过绑定Android原生已有的耳麦设备来完成audio音频策略选择,通过apk检测ble连接状态,通知audio服务耳麦设备的状态就可以使得录音通路切换至ble hal,实现从ble获取录音数据功能。 打补丁前最好使用干净的环境,不要有别家方案ble补丁,否则可能会有不兼容问题。 补丁如若不能使用首先检查节点是否存在和其权限,正常节点权限如下: ls -l /dev/hidraw* crw-rw---- 1 system audio 241, 0 2018-12-18 13:42 /dev/hidraw0 audio用户组有读写权限。 2、如果selinux模式为Enforcing,可以通过logcat搜索avc关键字。有如下类似提示则为异常,提示进程没有权限,检查sepolicy是否设置正常: avc: denied { read } for name="/" dev="tmpfs" ino=6145 scontext=u:r:mediaserver:s0 tcontext=u:object_r:device:s0 tclass=dir permissive=0 //Android 5.0和6.0版本,audio hal被mediaserver进程加载 avc: denied { read } for name="/" dev="tmpfs" ino=8125 scontext=u:r:audioserver:s0 tcontext=u:object_r:device:s0 tclass=dir permissive=0 //Android 7.0版本,audio hal被audioserver进程加载 avc: denied { read } for name="hidraw" dev="sysfs" ino=16332 scontext=u:r:hal_audio_default:s0 tcontext=u:object_r:sysfs:s0 tclass=dir permissive=0 //Android 8.0和9.0版本,audio hal被android.hardware.audio@2.0-service进程加载 3、检查audio的配置,打上patch后,首先确认小机上文件是否有修改到,目前文件可能位于/vendor/etc或/system/etc目录下,其中/vendor/etc下的配置文件是优先解析的。确保文件无误后,通过dumpsys media.audio_policy查看ble hal是否正常加载。 以下是相关说明: AudioPolicyManager: 0xf20c5200 Command Thread: 0xf20af140 Tones Thread: 0xf20af020 ... - Available input devices: Device 1: - id: 3 - type: AUDIO_DEVICE_IN_BUILTIN_MIC - Profiles: Profile 0: - format: AUDIO_FORMAT_PCM_16_BIT - sampling rates:8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000 - channel masks:0x000c, 0x0010 Device 2: - id: 20 - type: AUDIO_DEVICE_IN_WIRED_HEADSET //对应的数值是0x80000010 - name: RemoteDM1204 - Profiles: Available input devices指示当前可用设备,目前ble hal是和AUDIO_DEVICE_IN_WIRED_HEADSET设备绑定,如果需要录音走ble hal,AUDIO_DEVICE_IN_WIRED_HEADSET设备必须出现在可用设备中,如果没有,就可能是补丁中hidaudio.apk的问题。 HW Modules dump: ... - H

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值