通过Android应用以编程方式连接到蓝牙耳机

用户名

如何从我的Android应用程序连接到蓝牙耳机?我发现了很多有关发现蓝牙设备和配对的教程,而很少有关于连接的教程。

莫阿德尔

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

成功配对后,您将需要连接到设备以及HSP或HFP配置文件。请注意,如果没有HSP(耳机配置文件)或HFP(免提配置文件),您将无法连接呼叫并将其串流到耳机或扬声器。

我列出了步骤,以便您可以通过搜索每个步骤轻松找到更多详细信息。希望这对您有所帮助。

更新

我会尽力帮助您:在“ src”文件夹下添加一个名称为android.bluetooth的新包,然后创建IBluetoothHeadset.aidl

使用以下代码:

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>

这些是您必须添加到广泛的广播接收器中的过滤器。

ACL_CONNECTED表示何时连接了蓝牙,ACL_DISCONNECTED表示蓝牙断开了连接

对于特定的设备,您必须检查广播接收器中的意图/上下文

因此,您的新Receiver(包括以前的Receiver)将如下所示:

 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个权限:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

通过Android应用以编程方式连接到蓝牙耳机

来自分类Dev

以编程方式连接到蓝牙

来自分类Dev

以编程方式连接到蓝牙设备

来自分类Dev

在iOS中以编程方式连接蓝牙耳机吗?

来自分类Dev

如何将应用程序连接到蓝牙耳机

来自分类Dev

媒体播放器应以编程方式停止在我的android应用中断开耳机的连接

来自分类Dev

蓝牙耳机-无法连接到HSP / HFP

来自分类Dev

Android:通过蓝牙连接两个设备并以编程方式启用Internet访问

来自分类Dev

通过手机将蓝牙耳机连接到PC

来自分类Dev

无法应用网络设置,通过本机连接到蓝牙网络

来自分类Dev

只有配对后,Android应用才能通过蓝牙连接到Java应用服务器

来自分类Dev

是否可以通过编程方式连接到chromecast路由?

来自分类Dev

将Android应用重新连接到配对的蓝牙设备

来自分类Dev

将Android应用重新连接到配对的蓝牙设备

来自分类Dev

如何在Debian 8.1上连接到蓝牙耳机

来自分类Dev

无法连接到Ubuntu 16.04上的蓝牙耳机

来自分类Dev

如何在Debian 8.1上连接到蓝牙耳机

来自分类Dev

无法连接到Ubuntu 16.04上的蓝牙耳机

来自分类Dev

如何在Android上以编程方式配对和连接HID蓝牙设备(蓝牙键盘)

来自分类Dev

是否可以将Raspberry Pi 3上的Windows 10 IoT UWP C#应用程序连接到蓝牙耳机?

来自分类Dev

连接蓝牙 4.0 耳机

来自分类Dev

如果wifi名称匹配,Android以编程方式连接到Wifi

来自分类Dev

无法在Android上以编程方式连接到特定的Wifi网络

来自分类Dev

以编程方式连接到Android Q中的Wifi

来自分类Dev

一旦Windows 10 UWP中的连接断开,如何以编程方式连接到配对的蓝牙设备

来自分类Dev

通过终端连接到蓝牙设备

来自分类Dev

通过蓝牙连接到avd / logcat

来自分类Dev

以编程方式启用android蓝牙共享

来自分类Dev

通过BeeTee应用程序连接到其他蓝牙设备

Related 相关文章

  1. 1

    通过Android应用以编程方式连接到蓝牙耳机

  2. 2

    以编程方式连接到蓝牙

  3. 3

    以编程方式连接到蓝牙设备

  4. 4

    在iOS中以编程方式连接蓝牙耳机吗?

  5. 5

    如何将应用程序连接到蓝牙耳机

  6. 6

    媒体播放器应以编程方式停止在我的android应用中断开耳机的连接

  7. 7

    蓝牙耳机-无法连接到HSP / HFP

  8. 8

    Android:通过蓝牙连接两个设备并以编程方式启用Internet访问

  9. 9

    通过手机将蓝牙耳机连接到PC

  10. 10

    无法应用网络设置,通过本机连接到蓝牙网络

  11. 11

    只有配对后,Android应用才能通过蓝牙连接到Java应用服务器

  12. 12

    是否可以通过编程方式连接到chromecast路由?

  13. 13

    将Android应用重新连接到配对的蓝牙设备

  14. 14

    将Android应用重新连接到配对的蓝牙设备

  15. 15

    如何在Debian 8.1上连接到蓝牙耳机

  16. 16

    无法连接到Ubuntu 16.04上的蓝牙耳机

  17. 17

    如何在Debian 8.1上连接到蓝牙耳机

  18. 18

    无法连接到Ubuntu 16.04上的蓝牙耳机

  19. 19

    如何在Android上以编程方式配对和连接HID蓝牙设备(蓝牙键盘)

  20. 20

    是否可以将Raspberry Pi 3上的Windows 10 IoT UWP C#应用程序连接到蓝牙耳机?

  21. 21

    连接蓝牙 4.0 耳机

  22. 22

    如果wifi名称匹配,Android以编程方式连接到Wifi

  23. 23

    无法在Android上以编程方式连接到特定的Wifi网络

  24. 24

    以编程方式连接到Android Q中的Wifi

  25. 25

    一旦Windows 10 UWP中的连接断开,如何以编程方式连接到配对的蓝牙设备

  26. 26

    通过终端连接到蓝牙设备

  27. 27

    通过蓝牙连接到avd / logcat

  28. 28

    以编程方式启用android蓝牙共享

  29. 29

    通过BeeTee应用程序连接到其他蓝牙设备

热门标签

归档