Programmtically连接到蓝牙,从Android应用程序耳机 [英] Programmtically connect to bluetooth headset from android app

查看:727
本文介绍了Programmtically连接到蓝牙,从Android应用程序耳机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以指导我如何连接到从我的Andr​​oid应用程序的蓝牙耳机正确方向。我发现很多教程上发现的蓝牙设备和配对和极少数的连接。我尝试一些影片冯yputube但力帮助。有人可以给我解释一下或提供从那里我可以了解它的一些有用的链接?

Can someone guide me in correct direction on how to connect to bluetooth headset from my android app. I have found lots of tutorials on discovering bluetooth devices and pairing and very few on connecting. I tried few videos von yputube but dint help. Can someone explain me or provide some useful links from where i could learn about it ?

谢谢,我会很gratefull你的帮助,在此:)

Thanks I will be very gratefull to your help on this :)

推荐答案

首先,你需要确保蓝牙已启用然后搜索没有配对设备,然后使用设备地址,你配对的设备。对

First of All you need to make Sure Bluetooth is Enabled then Search for Unpaired Devices, Then Using Device Address, You pair the Device.

配对成功后,您将需要连接到设备,也HSP或HFP配置文件。 注意不HSP(耳机模式)或HFP(免提模式),您将无法连接和数据流调用您的耳机或扬声器。

After Successful Pairing you will need to connect to device and Also HSP or HFP profiles. Note without HSP(Headset Profile) or HFP(Hands-Free Profile) you won't be able to connect and stream calls to your Headset or Speaker.

我规定的步骤,你可以很容易地找到谷歌搜索的每个步骤的详细信息。 希望这可以帮助你。

I laid down the steps for you can easily find more details by googling each step. Hopefully this helps you.

更新

我会尽力帮助你多一点: 你必须在SRC文件夹中添加一个新包的名字:android.bluetooth 然后创建IBluetoothHeadset.aidl

I will try and help you bit more : you have to add a new package under "src" folder with name : android.bluetooth then create IBluetoothHeadset.aidl

与下面的code:

package android.bluetooth;

import android.bluetooth.BluetoothDevice;
interface IBluetoothHeadset {

// Public API
boolean connect(in BluetoothDevice device); //Api 11 and above
boolean connectHeadset(in BluetoothDevice device); // Below Api 11

boolean disconnect(in BluetoothDevice device);
boolean disconnectHeadset(in BluetoothDevice device);

List<BluetoothDevice> getConnectedDevices();
List<BluetoothDevice> getDevicesMatchingConnectionStates(in int[] states);
int getConnectionState(in BluetoothDevice device);
int getState(in BluetoothDevice device);
boolean setPriority(in BluetoothDevice device, int priority);
int getPriority(in BluetoothDevice device);
boolean startVoiceRecognition(in BluetoothDevice device);
boolean stopVoiceRecognition(in BluetoothDevice device);
boolean isAudioConnected(in BluetoothDevice device);
boolean sendVendorSpecificResultCode(in BluetoothDevice device,
                                     in String command,
                                     in String arg);

// APIs that can be made public in future
int getBatteryUsageHint(in BluetoothDevice device);

// Internal functions, not be made public
boolean acceptIncomingConnect(in BluetoothDevice device);
boolean rejectIncomingConnect(in BluetoothDevice device);
int getAudioState(in BluetoothDevice device);

boolean isAudioOn();
boolean connectAudio();
boolean disconnectAudio();
boolean startScoUsingVirtualVoiceCall(in BluetoothDevice device);
boolean stopScoUsingVirtualVoiceCall(in BluetoothDevice device);
void phoneStateChanged(int numActive, int numHeld, int callState, String number, int type);
void clccResponse(int index, int direction, int status, int mode, boolean mpty,
                  String number, int type);

}

然后在你的活动

     BluetoothDevice DeviceToConnect;
     IBluetoothHeadset ibth; 
     //IBluetoothHeadset instance used to connect and disconnect headset afterwards

     // Create and Register BroadCastListener for Action "HEADSET_INTERFACE_CONNECTED"
     // In Broadcast your code has to be something like that
     // if(ibth != null) ibth.connect(DeviceToConnect);


     //Then After Pairing DeviceToConnect;
     Intent i = new Intent(IBluetoothHeadset.class.getName());

    if (bindService(i, HSPConnection, Context.BIND_AUTO_CREATE)) {

    } else {
       Log.e("HSP FAILED", "Could not bind to Bluetooth HFP Service");
   }


      //Method for bind
  public static ServiceConnection HSPConnection= new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            ibth = IBluetoothHeadset.Stub.asInterface(service);
            //ibth instance used to connect and disconnect headset afterwards
            Intent intent = new Intent();
            intent.setAction("HEADSET_INTERFACE_CONNECTED");
            //same as the one we register earlier for broadcastreciever
                            ctx.sendBroadcast(intent);
            //ctx is Instance of Context
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            ibth=null;
        }

    };

更新2:

<intent-filter> 
  <action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
  <action android:name="android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED" />
  <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
</intent-filter>

这些都是你要添加到您的撒施接收器的过滤器。

These are the filters you have to add to your broad cast receiver.

ACL_CONNECTED信号,蓝牙连接和ACL_DISCONNECTED蓝牙信号断线时

ACL_CONNECTED signals when bluetooth is connected and ACL_DISCONNECTED signals bluetooth disconnection

有关你必须检查的意图/背景广播接收器专用设备

For specific device you have to check intents/context in broadcast receiver

所以你的新的接收器,包括previous看起来是这样的:

So your new Receiver including the previous will look something like that:

 BroadcastReceiver bcr = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
           //Device found
        }
        else if (BluetoothAdapter.ACTION_ACL_CONNECTED.equals(action)) {
           //Device is now connected
        }
        else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            //Done searching
        }
        else if (BluetoothAdapter.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
           //Device is about to disconnect
        }
        else if (BluetoothAdapter.ACTION_ACL_DISCONNECTED.equals(action)) {
           //Device has disconnected add whatever way u want to be notified here
           //e.g music-vibration-screen light
        }else if("HEADSET_INTERFACE_CONNECTED".equals(action){
           if(ibth != null) ibth.connect(DeviceToConnect);
        }
    }
};

我忘了补充一点,你需要在这些清单2权限:

I forgot to add that you need these 2 permissions in Manifest:

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />

这篇关于Programmtically连接到蓝牙,从Android应用程序耳机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆