安卓获得设备的名字像蓝牙设备怎么改名字名那样 不要型号名不是Build.MODEL

android.os.Build设备常量以及imsi号与ip地址的获得
android.os.Build.MODEL :设备名
android.os.Build.BRAND: 设备厂商
android.os.Build.VERSION.SDK:sdk版本号
一般用于版本兼容的检测或者其他功能
比如说coolpad 9930手机2.2的android系统
择MODEL为9930,BRAND为coolpad,sdk为8
imsi号获得
(TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE).getSubscriberId();
获取ip地址
WifiManager&wifiManager&=&(WifiManager)&getSystemService(WIFI_SERVICE);&&&&&
WifiInfo&wifiInfo&=&wifiManager.getConnectionInfo();&&&&&
int&ipAddress&=&wifiInfo.getIpAddress();&&&&&
String&ip&=&intToIp(ipAddress);&&&&&
public&String&intToIp(int&i)&{&&&&&&&
&&&return&((i&&&&24&)&&&0xFF&)&+&"."&+&&&&&
&&&&&&&&&&&&&&&((i&&&&16&)&&&0xFF)&+&"."&+&&&&&
&&&&&&&&&&&&&&&((i&&&&8&)&&&0xFF)&+&"."&+&&&&&
&&&&&&&&&&&&&&&(&i&&&0xFF)&;&&&&&
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。android 蓝牙耳机MIC音频输入
作者:用户
浏览:2244 次
android蓝牙耳机MIC音频输入10C已经将android和蓝牙耳机通过Headset协议连接上,但是耳机的MIC总是获取不到音频输入求大神指点。。。下面的编码,以在Android中启动语音识别:
android 蓝牙耳机MIC音频输入
已经将android和蓝牙耳机通过Headset协议连接上,但是耳机的MIC总是获取不到音频输入求大神指点 。。。
下面的编码,以在Android中启动语音识别:PackageManager pm = getPackageManager();List activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH) 0);if (activities.size() == 0) { displayWarning(""This device does not support speech recognition"");}Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);startActivityForResult(intent VOICE_RECOGNITION_REQUEST_CODE);这工作得很好。然而,它似乎并没有接受来自蓝牙耳机配对,而“手机音频”配置文件语音输入。 一,应用程序称为的SOUNDabout率有力的“Media音频”到“蓝牙(单声道)(SCO)”。有了这个程序集,我的语音识别现在可以从我的耳机追踪我的语音输入。 如何RecognizerIntent,并从蓝牙耳机语音输入? 我在API级别16看到有一个新的动作ACTION_VOICE_SEARCH_HANDS_FREE意图表单可供选择。这是太新了,但这样就解决了我的问题呢? 我一定要围绕淤泥在AudioManager类(就像我的SOUNDabout正在做)将音频路由setBluetoothScoOn()或startBluetoothSco()? +本文地址 :CodeGo.net/554937/ 许可清单 创建一个内部类BluetoothHelper extends BluetoothHeadSetUtils在你的Activity或Service。报告mBluetoothHelper并实例化它onCreate()BluetoothHelper mBluetoothH@Overridepublic void onCreate(){ mBluetoothHelper = new BluetoothHelper(this);} @OverrideonResume(){ mBluetoothHelper.start();}@OverrideonPause(){mBluetoothHelper.stop();}// inner class// BluetoothHeadsetUtils is an abstract class that has// 4 abstracts methods that need to be implemented.private class BluetoothHelper extends BluetoothHeadSetUtils{public BluetoothHelper(Context context){super(context);}@Overridepublic void onScoAudioDisconnected(){// Cancel speech recognizer if desired}@Overridepublic void onScoAudioConnected(){// Should start speech recognition here if not already started }@Overridepublic void onHeadsetDisconnected(){}@Overridepublic void onHeadsetConnected(){}}蓝牙耳机与文本语音转换,你需要设置的速度AudioManager类STREAM_VOICE_CALL在拨打电话之前说话。下面的销protected void speak(String text){HashMap myHashRender = new HashMap();if (mBluetoothHelper.isOnHeadsetSco()){myHashRender.put(TextToSpeech.Engine.KEY_PARAM_STREAM String.valueOf(AudioManager.STREAM_VOICE_CALL));}mTts.speak(text TextToSpeech.QUEUE_FLUSH myHashRender);}该BluetoothHeadsetUtils类复制到您的项目。import java.util.Limport android.annotation.SuppressLimport android.bluetooth.BluetoothAimport android.bluetooth.BluetoothCimport android.bluetooth.BluetoothDimport android.bluetooth.BluetoothHimport android.bluetooth.BluetoothPimport android.content.BroadcastRimport android.content.Cimport android.content.Iimport android.content.IntentFimport android.media.AudioMimport android.os.Bimport android.os.CountDownTimport android.util.L/**This is a utility to detect bluetooth headset connection and establish audio connectionfor android API &= 8. This includes a work around for API & 11 to detect already connected headsetbefore the application starts. This work around would only fails if Sco audioconnection is accepted but the connected device is not a headset.@author Hoan Nguyen*/public abstract class BluetoothHeadsetUtils{private Context mCprivate BluetoothAdapter mBluetoothAprivate BluetoothHeadset mBluetoothHprivate BluetoothDevice mConnectedHprivate AudioManager mAudioMprivate boolean mIsCountDownOn;private boolean mIsSprivate boolean mIsOnHeadsetSprivate boolean mIsSprivate static final String TAG = ""BluetoothHeadsetUtils""; //$NON-NLS-1$/*Constructor@param context/public BluetoothHeadsetUtils(Context context){mContext =mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);}/*Call this to start BluetoothHeadsetUtils functionalities.@return The return value of startBluetooth() or startBluetooth11()/public boolean start(){if (!mIsStarted){mIsStarted =if (Build.VERSION.SDK_INT & Build.VERSION_CODES.HONEYCOMB){mIsStarted = startBluetooth();}else{mIsStarted = startBluetooth11();}}return mIsS}/*Should call this on onResume or onDestroy.Unregister broadcast receivers and stop Sco audio connectionand cancel count down./public void stop(){if (mIsStarted){mIsStarted =if (Build.VERSION.SDK_INT & Build.VERSION_CODES.HONEYCOMB){stopBluetooth();}else{stopBluetooth11();}}}/*@return true if audio is connected through headset./public boolean isOnHeadsetSco(){return mIsOnHeadsetS}public abstract void onHeadsetDisconnected();public abstract void onHeadsetConnected();public abstract void onScoAudioDisconnected();public abstract void onScoAudioConnected();/*Register for bluetooth headset connection states and Sco audio states.Try to connect to bluetooth headset audio by calling startBluetoothSco(). This is a work around for API & 11 to detect if a headset is connected before the application starts.The official documentation for startBluetoothSco() states""This method can be used by applications wanting to send and received audio to/from a bluetooth SCO headset while the phone is not in call.""Does this mean that startBluetoothSco() would fail if the connected bluetooth deviceis not a headset?Thus if a call to startBluetoothSco() is successful i.e mBroadcastReceiver will receivean ACTION_SCO_AUDIO_STATE_CHANGED with intent extra SCO_AUDIO_STATE_CONNECTED thenwe assume that a headset is connected.@return false if device does not support bluetooth or current platform does not supports
use of SCO for off call./@SuppressWarnings(""deprecation"")private boolean startBluetooth(){Log.d(TAGstartBluetooth""); //$NON-NLS-1$// Device support bluetoothif (mBluetoothAdapter != null){if (mAudioManager.isBluetoothScoAvailableOffCall()){mContext.registerReceiver(mBroadcastReceiver
new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED));mContext.registerReceiver(mBroadcastReceiver
new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED));mContext.registerReceiver(mBroadcastReceiver
new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED));// Need to set audio mode to MODE_IN_CALL for call to startBluetoothSco() to succeed.mAudioManager.setMode(AudioManager.MODE_IN_CALL);mIsCountDownOn =// mCountDown repeatedly tries to start bluetooth Sco audio connection.mCountDown.start();// need for audio sco see mBroadcastReceivermIsStarting =}}}/*Register a headset profile listener@return false if device does not support bluetooth or current platform does not supports
use of SCO for off call or error in getting profile proxy./@TargetApi(Build.VERSION_CODES.HONEYCOMB)private boolean startBluetooth11(){Log.d(TAGstartBluetooth11""); //$NON-NLS-1$// Device support bluetoothif (mBluetoothAdapter != null){if (mAudioManager.isBluetoothScoAvailableOffCall()){// All the detection and audio connection are done in mHeadsetProfileListenerif (mBluetoothAdapter.getProfileProxy(mContext
mHeadsetProfileListener
BluetoothProfile.HEADSET)){}}}}/*API & 11Unregister broadcast receivers and stop Sco audio connectionand cancel count down./private void stopBluetooth(){Log.d(TAGstopBluetooth""); //$NON-NLS-1$if (mIsCountDownOn){mIsCountDownOn =mCountDown.cancel();}// Need to stop Sco audio connection here when the app// change orientation or close with headset still turns on.mContext.unregisterReceiver(mBroadcastReceiver);mAudioManager.stopBluetoothSco();mAudioManager.setMode(AudioManager.MODE_NORMAL);}/*API &= 11Unregister broadcast receivers and stop Sco audio connectionand cancel count down./@TargetApi(Build.VERSION_CODES.HONEYCOMB)protected void stopBluetooth11(){Log.d(TAGstopBluetooth11""); //$NON-NLS-1$if (mIsCountDownOn){mIsCountDownOn =mCountDown11.cancel();}if (mBluetoothHeadset != null){// Need to call stopVoiceRecognition here when the app// change orientation or close with headset still turns on.mBluetoothHeadset.stopVoiceRecognition(mConnectedHeadset);mContext.unregisterReceiver(mHeadsetBroadcastReceiver);mBluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET mBluetoothHeadset);mBluetoothHeadset =}}/*Broadcast receiver for API & 11Handle headset and Sco audio connection states./private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){@SuppressWarnings({""deprecation""synthetic-access""})@Overridepublic void onReceive(Context context Intent intent){String action = intent.getAction();if (action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)){mConnectedHeadset = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);BluetoothClass bluetoothClass = mConnectedHeadset.getBluetoothClass();if (bluetoothClass != null){// Check if device is a headset. Besides the 2 below are there other // device classes also qualified as headset?int deviceClass = bluetoothClass.getDeviceClass();if (deviceClass == BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE || deviceClass == BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET){// start bluetooth Sco audio connection.// Calling startBluetoothSco() always returns faIL here // that why a count down timer is implemented to call// startBluetoothSco() in the onTick.mAudioManager.setMode(AudioManager.MODE_IN_CALL);mIsCountDownOn =mCountDown.start();// override this if you want to do other thing when the device is connected.onHeadsetConnected();}}Log.d(TAG mConnectedHeadset.getName() + "" connected""); //$NON-NLS-1$}else if (action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)){Log.d(TAGHeadset disconnected""); //$NON-NLS-1$if (mIsCountDownOn){mIsCountDownOn =mCountDown.cancel();}mAudioManager.setMode(AudioManager.MODE_NORMAL);// override this if you want to do other thing when the device is disconnected.onHeadsetDisconnected();}else if (action.equals(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED)){int state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE
AudioManager.SCO_AUDIO_STATE_ERROR);if (state == AudioManager.SCO_AUDIO_STATE_CONNECTED){mIsOnHeadsetSco =if (mIsStarting){// When the device is connected before the application starts// ACTION_ACL_CONNECTED will not be received so call onHeadsetConnected heremIsStarting =onHeadsetConnected();}if (mIsCountDownOn){mIsCountDownOn =mCountDown.cancel();}// override this if you want to do other thing when Sco audio is connected.onScoAudioConnected();Log.d(TAGSco connected""); //$NON-NLS-1$}else if (state == AudioManager.SCO_AUDIO_STATE_DISCONNECTED){Log.d(TAGSco disconnected""); //$NON-NLS-1$// Always receive SCO_AUDIO_STATE_DISCONNECTED on call to startBluetooth()// which at that stage we do not want to do anything. Thus the if condition.if (!mIsStarting){mIsOnHeadsetSco =// Need to call stopBluetoothSco() otherwise startBluetoothSco()// will not be successful.mAudioManager.stopBluetoothSco();// override this if you want to do other thing when Sco audio is disconnected.onScoAudioDisconnected();} }}}};/*API & 11Try to connect to audio headset in onTick./private CountDownTimer mCountDown = new CountDownTimer(){@SuppressWarnings(""synthetic-access"")@Overridepublic void onTick(long millisUntilFinished){// When this call is successful this count down timer will be canceled.mAudioManager.startBluetoothSco();Log.d(TAGnonTick start bluetooth Sco""); //$NON-NLS-1$}@SuppressWarnings(""synthetic-access"")@Overridepublic void onFinish(){// Calls to startBluetoothSco() in onStick are not successful.// Should implement something to inform user of this failuremIsCountDownOn =mAudioManager.setMode(AudioManager.MODE_NORMAL);Log.d(TAGnonFinish fail to connect to headset audio""); //$NON-NLS-1$}};/*API &= 11Check for already connected headset and if so start audio connection.Register for broadcast of headset and Sco audio connection states./private BluetoothProfile.ServiceListener mHeadsetProfileListener = new BluetoothProfile.ServiceListener(){/*This method is never called even when we closeProfileProxy on onPause.When or will it ever be called???/@Overridepublic void onServiceDisconnected(int profile){Log.d(TAGProfile listener onServiceDisconnected""); //$NON-NLS-1$stopBluetooth11();}@SuppressWarnings(""synthetic-access"")@TargetApi(Build.VERSION_CODES.HONEYCOMB)@Overridepublic void onServiceConnected(int profile BluetoothProfile proxy){Log.d(TAGProfile listener onServiceConnected""); //$NON-NLS-1$// mBluetoothHeadset is just a headset profile // it does not represent a headset device.mBluetoothHeadset = (BluetoothHeadset)// If a headset is connected before this application starts// ACTION_CONNECTION_STATE_CHANGED will not be broadcast. // So we need to check for already connected headset.List devices = mBluetoothHeadset.getConnectedDevices();if (devices.size() & 0){// Only one headset can be connected at a time // so the connected headset is at index 0.mConnectedHeadset = devices.get(0);onHeadsetConnected();// Should not need count down timer but just in case.// See comment below in mHeadsetBroadcastReceiver onReceive()mIsCountDownOn =mCountDown11.start();Log.d(TAGStart count down""); //$NON-NLS-1$}// During the active life time of the app a user may turn on and off the headset.// So register for broadcast of connection states.mContext.registerReceiver(mHeadsetBroadcastReceiver new IntentFilter(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED));// Calling startVoiceRecognition does not result in immediate audio connection.// So register for broadcast of audio connection states. This broadcast will// only be sent if startVoiceRecognition returns true.mContext.registerReceiver(mHeadsetBroadcastReceiver new IntentFilter(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED));}};/*API &= 11Handle headset and Sco audio connection states./private BroadcastReceiver mHeadsetBroadcastReceiver = new BroadcastReceiver(){@SuppressWarnings(""synthetic-access"")@TargetApi(Build.VERSION_CODES.HONEYCOMB)@Overridepublic void onReceive(Context context Intent intent){String action = intent.getAction();if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)){state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE
BluetoothHeadset.STATE_DISCONNECTED);Log.d(TAGnAction = "" + action + ""nState = "" + state); //$NON-NLS-1$ //$NON-NLS-2$if (state == BluetoothHeadset.STATE_CONNECTED){mConnectedHeadset = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);// Calling startVoiceRecognition always returns false here // that why a count down timer is implemented to call// startVoiceRecognition in the onTick.mIsCountDownOn =mCountDown11.start();// override this if you want to do other thing when the device is connected.onHeadsetConnected();Log.d(TAGStart count down""); //$NON-NLS-1$}else if (state == BluetoothHeadset.STATE_DISCONNECTED){// Calling stopVoiceRecognition always returns false here// as it should since the headset is no longer connected.if (mIsCountDownOn){mIsCountDownOn =mCountDown11.cancel();}mConnectedHeadset =// override this if you want to do other thing when the device is disconnected.onHeadsetDisconnected();Log.d(TAGHeadset disconnected""); //$NON-NLS-1$}}else // audio{state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE BluetoothHeadset.STATE_AUDIO_DISCONNECTED);Log.d(TAGnAction = "" + action + ""nState = "" + state); //$NON-NLS-1$ //$NON-NLS-2$if (state == BluetoothHeadset.STATE_AUDIO_CONNECTED){Log.d(TAGnHeadset audio connected""); //$NON-NLS-1$mIsOnHeadsetSco =if (mIsCountDownOn){mIsCountDownOn =mCountDown11.cancel();}// override this if you want to do other thing when headset audio is connected.onScoAudioConnected();}else if (state == BluetoothHeadset.STATE_AUDIO_DISCONNECTED){mIsOnHeadsetSco =// The headset audio is disconnected but calling// stopVoiceRecognition always returns true here.mBluetoothHeadset.stopVoiceRecognition(mConnectedHeadset);// override this if you want to do other thing when headset audio is disconnected.onScoAudioDisconnected();Log.d(TAGHeadset audio disconnected""); //$NON-NLS-1$}} }};/*API &= 11Try to connect to audio headset in onTick.*/private CountDownTimer mCountDown11 = new CountDownTimer(){@TargetApi(Build.VERSION_CODES.HONEYCOMB)@SuppressWarnings(""synthetic-access"")@Overridepublic void onTick(long millisUntilFinished){// First stick calls always returns false. The second stick// always returns true if the countDownInterval is set to 1000.// It is somewhere in between 500 to a 1000.mBluetoothHeadset.startVoiceRecognition(mConnectedHeadset);Log.d(TAGonTick startVoiceRecognition""); //$NON-NLS-1$}@SuppressWarnings(""synthetic-access"")@Overridepublic void onFinish(){// Calls to startVoiceRecognition in onStick are not successful.// Should implement something to inform user of this failuremIsCountDownOn =Log.d(TAGnonFinish fail to connect to headset audio""); //$NON-NLS-1$}};} (日)编辑更改为@TargetApi(Build.VERSION_CODES.HONEYCOMB)必要时。如果你有问题,java.lang.NoClassDefFoundError的API,8或9,只是删除所有的API&=11码。该startBluetoothSco()API适用于所有版本。 + 尽管蓝牙耳机配对并连接到手机音频配置文件(HF / HS),实际的音频连接(SCO)当呼叫才成立和接受。 为你的应用VR从蓝牙耳机接收语音输入应用程序将不得不建立上海合作组织到VR耳机触发输入, 你需要以下- isBluetoothScoAvailableOffCall检查 CodeGo.net,如果平台支持此函数, startBluetoothSco和stopBluetoothSco启动上海合作组织的耳机。 + 我认为,所有你必须改变背面的音频设置为您的应用程序。 如果你推杆以下引脚当你想接收,并通过蓝牙从耳机发出的声音。AudioManager audioMaudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);audioManager.startBluetoothSco();audioManager.setBluetoothScoOn(true);不要忘记恢复以下列方式在手机上正常的设置当你的蓝牙。audioManager.setMode(AudioManager.MODE_NORMAL);audioManager.stopBluetoothSco();audioManager.setBluetoothScoOn(false);有需要的权限如下:这很容易! + startBluetoothSco需要长时间来建立,也就是如果你需要它的语音控制的问题。 有没有快速的方法来蓝牙mic来听录音,然后听完之后把它关掉? 如果连接的是对整个那么它是不可能通过流支持A2DP的音频。 密封式的,理想化的世界: 通过支持A2DP八月音频。当它开始监听,SCO蓝牙mic。任何回应又是支持A2DP。 事实上,如果它已经连接-你可以在飞行中通过改变流切换到流调用? DM跳,有没有明显的延迟? +本文标题 :使用Android RecognizerIntent与蓝牙耳机 +下面的编码,以在Android中启动语音识别:PackageManager pm = getPackageManager();List activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH) 0);if (activities.size() == 0) {displayWarning(""This device does not support speech recognition"");}Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);startActivityForResult(intent VOICE_RECOGNITION_REQUEST_CODE);这工作得很好。然而,它似乎并没有接受来自蓝牙耳机配对,而“手机音频”配置文件语音输入。 一,应用程序称为的SOUNDabout率有力的“Media音频”到“蓝牙(单声道)(SCO)”。有了这个程序集,我的语音识别现在可以从我的耳机追踪我的语音输入。 如何RecognizerIntent,并从蓝牙耳机语音输入? 我在API级别16看到有一个新的动作ACTION_VOICE_SEARCH_HANDS_FREE意图表单可供选择。这是太新了,但这样就解决了我的问题呢? 我一定要围绕淤泥在AudioManager类(就像我的SOUNDabout正在做)将音频路由setBluetoothScoOn()或startBluetoothSco()? +本文地址 :CodeGo.net/554937/ ------------------------------------------------------------------------------------------------------------------------- 许可清单 创建一个内部类BluetoothHelper extends BluetoothHeadSetUtils在你的Activity或Service。报告mBluetoothHelper并实例化它onCreate()BluetoothHelper mBluetoothH@Overridepublic void onCreate(){ mBluetoothHelper = new BluetoothHelper(this);} @OverrideonResume(){ mBluetoothHelper.start();}@OverrideonPause(){mBluetoothHelper.stop();}// inner class// BluetoothHeadsetUtils is an abstract class that has// 4 abstracts methods that need to be implemented.private class BluetoothHelper extends BluetoothHeadSetUtils{public BluetoothHelper(Context context){super(context);}@Overridepublic void onScoAudioDisconnected(){// Cancel speech recognizer if desired}@Overridepublic void onScoAudioConnected(){// Should start speech recognition here if not already started }@Overridepublic void onHeadsetDisconnected(){}@Overridepublic void onHeadsetConnected(){}}蓝牙耳机与文本语音转换,你需要设置的速度AudioManager类STREAM_VOICE_CALL在拨打电话之前说话。下面的销protected void speak(String text){HashMap myHashRender = new HashMap();if (mBluetoothHelper.isOnHeadsetSco()){myHashRender.put(TextToSpeech.Engine.KEY_PARAM_STREAM String.valueOf(AudioManager.STREAM_VOICE_CALL));}mTts.speak(text TextToSpeech.QUEUE_FLUSH myHashRender);}该BluetoothHeadsetUtils类复制到您的项目。import java.util.Limport android.annotation.SuppressLimport android.bluetooth.BluetoothAimport android.bluetooth.BluetoothCimport android.bluetooth.BluetoothDimport android.bluetooth.BluetoothHimport android.bluetooth.BluetoothPimport android.content.BroadcastRimport android.content.Cimport android.content.Iimport android.content.IntentFimport android.media.AudioMimport android.os.Bimport android.os.CountDownTimport android.util.L/**This is a utility to detect bluetooth headset connection and establish audio connectionfor android API &= 8. This includes a work around for API & 11 to detect already connected headsetbefore the application starts. This work around would only fails if Sco audioconnection is accepted but the connected device is not a headset.@author Hoan Nguyen*/public abstract class BluetoothHeadsetUtils{private Context mCprivate BluetoothAdapter mBluetoothAprivate BluetoothHeadset mBluetoothHprivate BluetoothDevice mConnectedHprivate AudioManager mAudioMprivate boolean mIsCountDownOn;private boolean mIsSprivate boolean mIsOnHeadsetSprivate boolean mIsSprivate static final String TAG = ""BluetoothHeadsetUtils""; //$NON-NLS-1$/*Constructor@param context/public BluetoothHeadsetUtils(Context context){mContext =mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);}/*Call this to start BluetoothHeadsetUtils functionalities.@return The return value of startBluetooth() or startBluetooth11()/public boolean start(){if (!mIsStarted){mIsStarted =if (Build.VERSION.SDK_INT & Build.VERSION_CODES.HONEYCOMB){mIsStarted = startBluetooth();}else{mIsStarted = startBluetooth11();}}return mIsS}/*Should call this on onResume or onDestroy.Unregister broadcast receivers and stop Sco audio connectionand cancel count down./public void stop(){if (mIsStarted){mIsStarted =if (Build.VERSION.SDK_INT & Build.VERSION_CODES.HONEYCOMB){stopBluetooth();}else{stopBluetooth11();}}}/*@return true if audio is connected through headset./public boolean isOnHeadsetSco(){return mIsOnHeadsetS}public abstract void onHeadsetDisconnected();public abstract void onHeadsetConnected();public abstract void onScoAudioDisconnected();public abstract void onScoAudioConnected();/*Register for bluetooth headset connection states and Sco audio states.Try to connect to bluetooth headset audio by calling startBluetoothSco(). This is a work around for API & 11 to detect if a headset is connected before the application starts.The official documentation for startBluetoothSco() states""This method can be used by applications wanting to send and received audio to/from a bluetooth SCO headset while the phone is not in call.""Does this mean that startBluetoothSco() would fail if the connected bluetooth deviceis not a headset?Thus if a call to startBluetoothSco() is successful i.e mBroadcastReceiver will receivean ACTION_SCO_AUDIO_STATE_CHANGED with intent extra SCO_AUDIO_STATE_CONNECTED thenwe assume that a headset is connected.@return false if device does not support bluetooth or current platform does not supports
use of SCO for off call./@SuppressWarnings(""deprecation"")private boolean startBluetooth(){Log.d(TAGstartBluetooth""); //$NON-NLS-1$// Device support bluetoothif (mBluetoothAdapter != null){if (mAudioManager.isBluetoothScoAvailableOffCall()){mContext.registerReceiver(mBroadcastReceiver
new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED));mContext.registerReceiver(mBroadcastReceiver
new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED));mContext.registerReceiver(mBroadcastReceiver
new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED));// Need to set audio mode to MODE_IN_CALL for call to startBluetoothSco() to succeed.mAudioManager.setMode(AudioManager.MODE_IN_CALL);mIsCountDownOn =// mCountDown repeatedly tries to start bluetooth Sco audio connection.mCountDown.start();// need for audio sco see mBroadcastReceivermIsStarting =}}}/*Register a headset profile listener@return false if device does not support bluetooth or current platform does not supports
use of SCO for off call or error in getting profile proxy./@TargetApi(Build.VERSION_CODES.HONEYCOMB)private boolean startBluetooth11(){Log.d(TAGstartBluetooth11""); //$NON-NLS-1$// Device support bluetoothif (mBluetoothAdapter != null){if (mAudioManager.isBluetoothScoAvailableOffCall()){// All the detection and audio connection are done in mHeadsetProfileListenerif (mBluetoothAdapter.getProfileProxy(mContext
mHeadsetProfileListener
BluetoothProfile.HEADSET)){}}}}/*API & 11Unregister broadcast receivers and stop Sco audio connectionand cancel count down./private void stopBluetooth(){Log.d(TAGstopBluetooth""); //$NON-NLS-1$if (mIsCountDownOn){mIsCountDownOn =mCountDown.cancel();}// Need to stop Sco audio connection here when the app// change orientation or close with headset still turns on.mContext.unregisterReceiver(mBroadcastReceiver);mAudioManager.stopBluetoothSco();mAudioManager.setMode(AudioManager.MODE_NORMAL);}/*API &= 11Unregister broadcast receivers and stop Sco audio connectionand cancel count down./@TargetApi(Build.VERSION_CODES.HONEYCOMB)protected void stopBluetooth11(){Log.d(TAGstopBluetooth11""); //$NON-NLS-1$if (mIsCountDownOn){mIsCountDownOn =mCountDown11.cancel();}if (mBluetoothHeadset != null){// Need to call stopVoiceRecognition here when the app// change orientation or close with headset still turns on.mBluetoothHeadset.stopVoiceRecognition(mConnectedHeadset);mContext.unregisterReceiver(mHeadsetBroadcastReceiver);mBluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET mBluetoothHeadset);mBluetoothHeadset =}}/*Broadcast receiver for API & 11Handle headset and Sco audio connection states./private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){@SuppressWarnings({""deprecation""synthetic-access""})@Overridepublic void onReceive(Context context Intent intent){String action = intent.getAction();if (action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)){mConnectedHeadset = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);BluetoothClass bluetoothClass = mConnectedHeadset.getBluetoothClass();if (bluetoothClass != null){// Check if device is a headset. Besides the 2 below are there other // device classes also qualified as headset?int deviceClass = bluetoothClass.getDeviceClass();if (deviceClass == BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE || deviceClass == BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET){// start bluetooth Sco audio connection.// Calling startBluetoothSco() always returns faIL here // that why a count down timer is implemented to call// startBluetoothSco() in the onTick.mAudioManager.setMode(AudioManager.MODE_IN_CALL);mIsCountDownOn =mCountDown.start();// override this if you want to do other thing when the device is connected.onHeadsetConnected();}}Log.d(TAG mConnectedHeadset.getName() + "" connected""); //$NON-NLS-1$}else if (action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)){Log.d(TAGHeadset disconnected""); //$NON-NLS-1$if (mIsCountDownOn){mIsCountDownOn =mCountDown.cancel();}mAudioManager.setMode(AudioManager.MODE_NORMAL);// override this if you want to do other thing when the device is disconnected.onHeadsetDisconnected();}else if (action.equals(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED)){int state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE
AudioManager.SCO_AUDIO_STATE_ERROR);if (state == AudioManager.SCO_AUDIO_STATE_CONNECTED){mIsOnHeadsetSco =if (mIsStarting){// When the device is connected before the application starts// ACTION_ACL_CONNECTED will not be received so call onHeadsetConnected heremIsStarting =onHeadsetConnected();}if (mIsCountDownOn){mIsCountDownOn =mCountDown.cancel();}// override this if you want to do other thing when Sco audio is connected.onScoAudioConnected();Log.d(TAGSco connected""); //$NON-NLS-1$}else if (state == AudioManager.SCO_AUDIO_STATE_DISCONNECTED){Log.d(TAGSco disconnected""); //$NON-NLS-1$// Always receive SCO_AUDIO_STATE_DISCONNECTED on call to startBluetooth()// which at that stage we do not want to do anything. Thus the if condition.if (!mIsStarting){mIsOnHeadsetSco =// Need to call stopBluetoothSco() otherwise startBluetoothSco()// will not be successful.mAudioManager.stopBluetoothSco();// override this if you want to do other thing when Sco audio is disconnected.onScoAudioDisconnected();} }}}};/*API & 11Try to connect to audio headset in onTick./private CountDownTimer mCountDown = new CountDownTimer(){@SuppressWarnings(""synthetic-access"")@Overridepublic void onTick(long millisUntilFinished){// When this call is successful this count down timer will be canceled.mAudioManager.startBluetoothSco();Log.d(TAGnonTick start bluetooth Sco""); //$NON-NLS-1$}@SuppressWarnings(""synthetic-access"")@Overridepublic void onFinish(){// Calls to startBluetoothSco() in onStick are not successful.// Should implement something to inform user of this failuremIsCountDownOn =mAudioManager.setMode(AudioManager.MODE_NORMAL);Log.d(TAGnonFinish fail to connect to headset audio""); //$NON-NLS-1$}};/*API &= 11Check for already connected headset and if so start audio connection.Register for broadcast of headset and Sco audio connection states./private BluetoothProfile.ServiceListener mHeadsetProfileListener = new BluetoothProfile.ServiceListener(){/*This method is never called even when we closeProfileProxy on onPause.When or will it ever be called???/@Overridepublic void onServiceDisconnected(int profile){Log.d(TAGProfile listener onServiceDisconnected""); //$NON-NLS-1$stopBluetooth11();}@SuppressWarnings(""synthetic-access"")@TargetApi(Build.VERSION_CODES.HONEYCOMB)@Overridepublic void onServiceConnected(int profile BluetoothProfile proxy){Log.d(TAGProfile listener onServiceConnected""); //$NON-NLS-1$// mBluetoothHeadset is just a headset profile // it does not represent a headset device.mBluetoothHeadset = (BluetoothHeadset)// If a headset is connected before this application starts// ACTION_CONNECTION_STATE_CHANGED will not be broadcast. // So we need to check for already connected headset.List devices = mBluetoothHeadset.getConnectedDevices();if (devices.size() & 0){// Only one headset can be connected at a time // so the connected headset is at index 0.mConnectedHeadset = devices.get(0);onHeadsetConnected();// Should not need count down timer but just in case.// See comment below in mHeadsetBroadcastReceiver onReceive()mIsCountDownOn =mCountDown11.start();Log.d(TAGStart count down""); //$NON-NLS-1$}// During the active life time of the app a user may turn on and off the headset.// So register for broadcast of connection states.mContext.registerReceiver(mHeadsetBroadcastReceiver new IntentFilter(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED));// Calling startVoiceRecognition does not result in immediate audio connection.// So register for broadcast of audio connection states. This broadcast will// only be sent if startVoiceRecognition returns true.mContext.registerReceiver(mHeadsetBroadcastReceiver new IntentFilter(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED));}};/*API &= 11Handle headset and Sco audio connection states./private BroadcastReceiver mHeadsetBroadcastReceiver = new BroadcastReceiver(){@SuppressWarnings(""synthetic-access"")@TargetApi(Build.VERSION_CODES.HONEYCOMB)@Overridepublic void onReceive(Context context Intent intent){String action = intent.getAction();if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)){state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE
BluetoothHeadset.STATE_DISCONNECTED);Log.d(TAGnAction = "" + action + ""nState = "" + state); //$NON-NLS-1$ //$NON-NLS-2$if (state == BluetoothHeadset.STATE_CONNECTED){mConnectedHeadset = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);// Calling startVoiceRecognition always returns false here // that why a count down timer is implemented to call// startVoiceRecognition in the onTick.mIsCountDownOn =mCountDown11.start();// override this if you want to do other thing when the device is connected.onHeadsetConnected();Log.d(TAGStart count down""); //$NON-NLS-1$}else if (state == BluetoothHeadset.STATE_DISCONNECTED){// Calling stopVoiceRecognition always returns false here// as it should since the headset is no longer connected.if (mIsCountDownOn){mIsCountDownOn =mCountDown11.cancel();}mConnectedHeadset =// override this if you want to do other thing when the device is disconnected.onHeadsetDisconnected();Log.d(TAGHeadset disconnected""); //$NON-NLS-1$}}else // audio{state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE BluetoothHeadset.STATE_AUDIO_DISCONNECTED);Log.d(TAGnAction = "" + action + ""nState = "" + state); //$NON-NLS-1$ //$NON-NLS-2$if (state == BluetoothHeadset.STATE_AUDIO_CONNECTED){Log.d(TAGnHeadset audio connected""); //$NON-NLS-1$mIsOnHeadsetSco =if (mIsCountDownOn){mIsCountDownOn =mCountDown11.cancel();}// override this if you want to do other thing when headset audio is connected.onScoAudioConnected();}else if (state == BluetoothHeadset.STATE_AUDIO_DISCONNECTED){mIsOnHeadsetSco =// The headset audio is disconnected but calling// stopVoiceRecognition always returns true here.mBluetoothHeadset.stopVoiceRecognition(mConnectedHeadset);// override this if you want to do other thing when headset audio is disconnected.onScoAudioDisconnected();Log.d(TAGHeadset audio disconnected""); //$NON-NLS-1$}} }};/*API &= 11Try to connect to audio headset in onTick.*/private CountDownTimer mCountDown11 = new CountDownTimer(){@TargetApi(Build.VERSION_CODES.HONEYCOMB)@SuppressWarnings(""synthetic-access"")@Overridepublic void onTick(long millisUntilFinished){// First stick calls always returns false. The second stick// always returns true if the countDownInterval is set to 1000.// It is somewhere in between 500 to a 1000.mBluetoothHeadset.startVoiceRecognition(mConnectedHeadset);Log.d(TAGonTick startVoiceRecognition""); //$NON-NLS-1$}@SuppressWarnings(""synthetic-access"")@Overridepublic void onFinish(){// Calls to startVoiceRecognition in onStick are not successful.// Should implement something to inform user of this failuremIsCountDownOn =Log.d(TAGnonFinish fail to connect to headset audio""); //$NON-NLS-1$}};} (日)编辑更改为@TargetApi(Build.VERSION_CODES.HONEYCOMB)必要时。如果你有问题,java.lang.NoClassDefFoundError的API,8或9,只是删除所有的API&=11码。该startBluetoothSco()API适用于所有版本。 + 尽管蓝牙耳机配对并连接到手机音频配置文件(HF / HS),实际的音频连接(SCO)当呼叫才成立和接受。 为你的应用VR从蓝牙耳机接收语音输入应用程序将不得不建立上海合作组织到VR耳机触发输入, 你需要以下- isBluetoothScoAvailableOffCall检查 CodeGo.net,如果平台支持此函数, startBluetoothSco和stopBluetoothSco启动上海合作组织的耳机。 + 我认为,所有你必须改变背面的音频设置为您的应用程序。 如果你推杆以下引脚当你想接收,并通过蓝牙从耳机发出的声音。AudioManager audioMaudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);audioManager.startBluetoothSco();audioManager.setBluetoothScoOn(true);不要忘记恢复以下列方式在手机上正常的设置当你的蓝牙。audioManager.setMode(AudioManager.MODE_NORMAL);audioManager.stopBluetoothSco();audioManager.setBluetoothScoOn(false);有需要的权限如下:这很容易! + startBluetoothSco需要长时间来建立,也就是如果你需要它的语音控制的问题。 有没有快速的方法来蓝牙mic来听录音,然后听完之后把它关掉? 如果连接的是对整个那么它是不可能通过流支持A2DP的音频。 密封式的,理想化的世界: 通过支持A2DP八月音频。当它开始监听,SCO蓝牙mic。任何回应又是支持A2DP。 事实上,如果它已经连接-你可以在飞行中通过改变流切换到流调用? DM跳,有没有明显的延迟? +本文标题 :使用Android RecognizerIntent与蓝牙耳机 +
解决方案二:
下面的编码,以在Android中启动语音识别:PackageManager pm = getPackageManager();List activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH) 0);if (activities.size() == 0) { displayWarning(""This device does not support speech recognition"");}Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);startActivityForResult(intent VOICE_RECOGNITION_REQUEST_CODE);这工作得很好。然而,它似乎并没有接受来自蓝牙耳机配对,而“手机音频”配置文件语音输入。 一,应用程序称为的SOUNDabout率有力的“Media音频”到“蓝牙(单声道)(SCO)”。有了这个程序集,我的语音识别现在可以从我的耳机追踪我的语音输入。 如何RecognizerIntent,并从蓝牙耳机语音输入? 我在API级别16看到有一个新的动作ACTION_VOICE_SEARCH_HANDS_FREE意图表单可供选择。这是太新了,但这样就解决了我的问题呢? 我一定要围绕淤泥在AudioManager类(就像我的SOUNDabout正在做)将音频路由setBluetoothScoOn()或startBluetoothSco()? +本文地址 :CodeGo.net/554937/ 许可清单 创建一个内部类BluetoothHelper extends BluetoothHeadSetUtils在你的Activity或Service。报告mBluetoothHelper并实例化它onCreate()BluetoothHelper mBluetoothH@Overridepublic void onCreate(){ mBluetoothHelper = new BluetoothHelper(this);} @OverrideonResume(){ mBluetoothHelper.start();}@OverrideonPause(){mBluetoothHelper.stop();}// inner class// BluetoothHeadsetUtils is an abstract class that has// 4 abstracts methods that need to be implemented.private class BluetoothHelper extends BluetoothHeadSetUtils{public BluetoothHelper(Context context){super(context);}@Overridepublic void onScoAudioDisconnected(){// Cancel speech recognizer if desired}@Overridepublic void onScoAudioConnected(){// Should start speech recognition here if not already started }@Overridepublic void onHeadsetDisconnected(){}@Overridepublic void onHeadsetConnected(){}}蓝牙耳机与文本语音转换,你需要设置的速度AudioManager类STREAM_VOICE_CALL在拨打电话之前说话。下面的销protected void speak(String text){HashMap myHashRender = new HashMap();if (mBluetoothHelper.isOnHeadsetSco()){myHashRender.put(TextToSpeech.Engine.KEY_PARAM_STREAM String.valueOf(AudioManager.STREAM_VOICE_CALL));}mTts.speak(text TextToSpeech.QUEUE_FLUSH myHashRender);}该BluetoothHeadsetUtils类复制到您的项目。import java.util.Limport android.annotation.SuppressLimport android.bluetooth.BluetoothAimport android.bluetooth.BluetoothCimport android.bluetooth.BluetoothDimport android.bluetooth.BluetoothHimport android.bluetooth.BluetoothPimport android.content.BroadcastRimport android.content.Cimport android.content.Iimport android.content.IntentFimport android.media.AudioMimport android.os.Bimport android.os.CountDownTimport android.util.L/**This is a utility to detect bluetooth headset connection and establish audio connectionfor android API &= 8. This includes a work around for API & 11 to detect already connected headsetbefore the application starts. This work around would only fails if Sco audioconnection is accepted but the connected device is not a headset.@author Hoan Nguyen*/public abstract class BluetoothHeadsetUtils{private Context mCprivate BluetoothAdapter mBluetoothAprivate BluetoothHeadset mBluetoothHprivate BluetoothDevice mConnectedHprivate AudioManager mAudioMprivate boolean mIsCountDownOn;private boolean mIsSprivate boolean mIsOnHeadsetSprivate boolean mIsSprivate static final String TAG = ""BluetoothHeadsetUtils""; //$NON-NLS-1$/*Constructor@param context/public BluetoothHeadsetUtils(Context context){mContext =mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);}/*Call this to start BluetoothHeadsetUtils functionalities.@return The return value of startBluetooth() or startBluetooth11()/public boolean start(){if (!mIsStarted){mIsStarted =if (Build.VERSION.SDK_INT & Build.VERSION_CODES.HONEYCOMB){mIsStarted = startBluetooth();}else{mIsStarted = startBluetooth11();}}return mIsS}/*Should call this on onResume or onDestroy.Unregister broadcast receivers and stop Sco audio connectionand cancel count down./public void stop(){if (mIsStarted){mIsStarted =if (Build.VERSION.SDK_INT & Build.VERSION_CODES.HONEYCOMB){stopBluetooth();}else{stopBluetooth11();}}}/*@return true if audio is connected through headset./public boolean isOnHeadsetSco(){return mIsOnHeadsetS}public abstract void onHeadsetDisconnected();public abstract void onHeadsetConnected();public abstract void onScoAudioDisconnected();public abstract void onScoAudioConnected();/*Register for bluetooth headset connection states and Sco audio states.Try to connect to bluetooth headset audio by calling startBluetoothSco(). This is a work around for API & 11 to detect if a headset is connected before the application starts.The official documentation for startBluetoothSco() states""This method can be used by applications wanting to send and received audio to/from a bluetooth SCO headset while the phone is not in call.""Does this mean that startBluetoothSco() would fail if the connected bluetooth deviceis not a headset?Thus if a call to startBluetoothSco() is successful i.e mBroadcastReceiver will receivean ACTION_SCO_AUDIO_STATE_CHANGED with intent extra SCO_AUDIO_STATE_CONNECTED thenwe assume that a headset is connected.@return false if device does not support bluetooth or current platform does not supports
use of SCO for off call./@SuppressWarnings(""deprecation"")private boolean startBluetooth(){Log.d(TAGstartBluetooth""); //$NON-NLS-1$// Device support bluetoothif (mBluetoothAdapter != null){if (mAudioManager.isBluetoothScoAvailableOffCall()){mContext.registerReceiver(mBroadcastReceiver
new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED));mContext.registerReceiver(mBroadcastReceiver
new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED));mContext.registerReceiver(mBroadcastReceiver
new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED));// Need to set audio mode to MODE_IN_CALL for call to startBluetoothSco() to succeed.mAudioManager.setMode(AudioManager.MODE_IN_CALL);mIsCountDownOn =// mCountDown repeatedly tries to start bluetooth Sco audio connection.mCountDown.start();// need for audio sco see mBroadcastReceivermIsStarting =}}}/*Register a headset profile listener@return false if device does not support bluetooth or current platform does not supports
use of SCO for off call or error in getting profile proxy./@TargetApi(Build.VERSION_CODES.HONEYCOMB)private boolean startBluetooth11(){Log.d(TAGstartBluetooth11""); //$NON-NLS-1$// Device support bluetoothif (mBluetoothAdapter != null){if (mAudioManager.isBluetoothScoAvailableOffCall()){// All the detection and audio connection are done in mHeadsetProfileListenerif (mBluetoothAdapter.getProfileProxy(mContext
mHeadsetProfileListener
BluetoothProfile.HEADSET)){}}}}/*API & 11Unregister broadcast receivers and stop Sco audio connectionand cancel count down./private void stopBluetooth(){Log.d(TAGstopBluetooth""); //$NON-NLS-1$if (mIsCountDownOn){mIsCountDownOn =mCountDown.cancel();}// Need to stop Sco audio connection here when the app// change orientation or close with headset still turns on.mContext.unregisterReceiver(mBroadcastReceiver);mAudioManager.stopBluetoothSco();mAudioManager.setMode(AudioManager.MODE_NORMAL);}/*API &= 11Unregister broadcast receivers and stop Sco audio connectionand cancel count down./@TargetApi(Build.VERSION_CODES.HONEYCOMB)protected void stopBluetooth11(){Log.d(TAGstopBluetooth11""); //$NON-NLS-1$if (mIsCountDownOn){mIsCountDownOn =mCountDown11.cancel();}if (mBluetoothHeadset != null){// Need to call stopVoiceRecognition here when the app// change orientation or close with headset still turns on.mBluetoothHeadset.stopVoiceRecognition(mConnectedHeadset);mContext.unregisterReceiver(mHeadsetBroadcastReceiver);mBluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET mBluetoothHeadset);mBluetoothHeadset =}}/*Broadcast receiver for API & 11Handle headset and Sco audio connection states./private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){@SuppressWarnings({""deprecation""synthetic-access""})@Overridepublic void onReceive(Context context Intent intent){String action = intent.getAction();if (action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)){mConnectedHeadset = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);BluetoothClass bluetoothClass = mConnectedHeadset.getBluetoothClass();if (bluetoothClass != null){// Check if device is a headset. Besides the 2 below are there other // device classes also qualified as headset?int deviceClass = bluetoothClass.getDeviceClass();if (deviceClass == BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE || deviceClass == BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET){// start bluetooth Sco audio connection.// Calling startBluetoothSco() always returns faIL here // that why a count down timer is implemented to call// startBluetoothSco() in the onTick.mAudioManager.setMode(AudioManager.MODE_IN_CALL);mIsCountDownOn =mCountDown.start();// override this if you want to do other thing when the device is connected.onHeadsetConnected();}}Log.d(TAG mConnectedHeadset.getName() + "" connected""); //$NON-NLS-1$}else if (action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)){Log.d(TAGHeadset disconnected""); //$NON-NLS-1$if (mIsCountDownOn){mIsCountDownOn =mCountDown.cancel();}mAudioManager.setMode(AudioManager.MODE_NORMAL);// override this if you want to do other thing when the device is disconnected.onHeadsetDisconnected();}else if (action.equals(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED)){int state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE
AudioManager.SCO_AUDIO_STATE_ERROR);if (state == AudioManager.SCO_AUDIO_STATE_CONNECTED){mIsOnHeadsetSco =if (mIsStarting){// When the device is connected before the application starts// ACTION_ACL_CONNECTED will not be received so call onHeadsetConnected heremIsStarting =onHeadsetConnected();}if (mIsCountDownOn){mIsCountDownOn =mCountDown.cancel();}// override this if you want to do other thing when Sco audio is connected.onScoAudioConnected();Log.d(TAGSco connected""); //$NON-NLS-1$}else if (state == AudioManager.SCO_AUDIO_STATE_DISCONNECTED){Log.d(TAGSco disconnected""); //$NON-NLS-1$// Always receive SCO_AUDIO_STATE_DISCONNECTED on call to startBluetooth()// which at that stage we do not want to do anything. Thus the if condition.if (!mIsStarting){mIsOnHeadsetSco =// Need to call stopBluetoothSco() otherwise startBluetoothSco()// will not be successful.mAudioManager.stopBluetoothSco();// override this if you want to do other thing when Sco audio is disconnected.onScoAudioDisconnected();} }}}};/*API & 11Try to connect to audio headset in onTick./private CountDownTimer mCountDown = new CountDownTimer(){@SuppressWarnings(""synthetic-access"")@Overridepublic void onTick(long millisUntilFinished){// When this call is successful this count down timer will be canceled.mAudioManager.startBluetoothSco();Log.d(TAGnonTick start bluetooth Sco""); //$NON-NLS-1$}@SuppressWarnings(""synthetic-access"")@Overridepublic void onFinish(){// Calls to startBluetoothSco() in onStick are not successful.// Should implement something to inform user of this failuremIsCountDownOn =mAudioManager.setMode(AudioManager.MODE_NORMAL);Log.d(TAGnonFinish fail to connect to headset audio""); //$NON-NLS-1$}};/*API &= 11Check for already connected headset and if so start audio connection.Register for broadcast of headset and Sco audio connection states./private BluetoothProfile.ServiceListener mHeadsetProfileListener = new BluetoothProfile.ServiceListener(){/*This method is never called even when we closeProfileProxy on onPause.When or will it ever be called???/@Overridepublic void onServiceDisconnected(int profile){Log.d(TAGProfile listener onServiceDisconnected""); //$NON-NLS-1$stopBluetooth11();}@SuppressWarnings(""synthetic-access"")@TargetApi(Build.VERSION_CODES.HONEYCOMB)@Overridepublic void onServiceConnected(int profile BluetoothProfile proxy){Log.d(TAGProfile listener onServiceConnected""); //$NON-NLS-1$// mBluetoothHeadset is just a headset profile // it does not represent a headset device.mBluetoothHeadset = (BluetoothHeadset)// If a headset is connected before this application starts// ACTION_CONNECTION_STATE_CHANGED will not be broadcast. // So we need to check for already connected headset.List devices = mBluetoothHeadset.getConnectedDevices();if (devices.size() & 0){// Only one headset can be connected at a time // so the connected headset is at index 0.mConnectedHeadset = devices.get(0);onHeadsetConnected();// Should not need count down timer but just in case.// See comment below in mHeadsetBroadcastReceiver onReceive()mIsCountDownOn =mCountDown11.start();Log.d(TAGStart count down""); //$NON-NLS-1$}// During the active life time of the app a user may turn on and off the headset.// So register for broadcast of connection states.mContext.registerReceiver(mHeadsetBroadcastReceiver new IntentFilter(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED));// Calling startVoiceRecognition does not result in immediate audio connection.// So register for broadcast of audio connection states. This broadcast will// only be sent if startVoiceRecognition returns true.mContext.registerReceiver(mHeadsetBroadcastReceiver new IntentFilter(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED));}};/*API &= 11Handle headset and Sco audio connection states./private BroadcastReceiver mHeadsetBroadcastReceiver = new BroadcastReceiver(){@SuppressWarnings(""synthetic-access"")@TargetApi(Build.VERSION_CODES.HONEYCOMB)@Overridepublic void onReceive(Context context Intent intent){String action = intent.getAction();if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)){state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE
BluetoothHeadset.STATE_DISCONNECTED);Log.d(TAGnAction = "" + action + ""nState = "" + state); //$NON-NLS-1$ //$NON-NLS-2$if (state == BluetoothHeadset.STATE_CONNECTED){mConnectedHeadset = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);// Calling startVoiceRecognition always returns false here // that why a count down timer is implemented to call// startVoiceRecognition in the onTick.mIsCountDownOn =mCountDown11.start();// override this if you want to do other thing when the device is connected.onHeadsetConnected();Log.d(TAGStart count down""); //$NON-NLS-1$}else if (state == BluetoothHeadset.STATE_DISCONNECTED){// Calling stopVoiceRecognition always returns false here// as it should since the headset is no longer connected.if (mIsCountDownOn){mIsCountDownOn =mCountDown11.cancel();}mConnectedHeadset =// override this if you want to do other thing when the device is disconnected.onHeadsetDisconnected();Log.d(TAGHeadset disconnected""); //$NON-NLS-1$}}else // audio{state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE BluetoothHeadset.STATE_AUDIO_DISCONNECTED);Log.d(TAGnAction = "" + action + ""nState = "" + state); //$NON-NLS-1$ //$NON-NLS-2$if (state == BluetoothHeadset.STATE_AUDIO_CONNECTED){Log.d(TAGnHeadset audio connected""); //$NON-NLS-1$mIsOnHeadsetSco =if (mIsCountDownOn){mIsCountDownOn =mCountDown11.cancel();}// override this if you want to do other thing when headset audio is connected.onScoAudioConnected();}else if (state == BluetoothHeadset.STATE_AUDIO_DISCONNECTED){mIsOnHeadsetSco =// The headset audio is disconnected but calling// stopVoiceRecognition always returns true here.mBluetoothHeadset.stopVoiceRecognition(mConnectedHeadset);// override this if you want to do other thing when headset audio is disconnected.onScoAudioDisconnected();Log.d(TAGHeadset audio disconnected""); //$NON-NLS-1$}} }};/*API &= 11Try to connect to audio headset in onTick.*/private CountDownTimer mCountDown11 = new CountDownTimer(){@TargetApi(Build.VERSION_CODES.HONEYCOMB)@SuppressWarnings(""synthetic-access"")@Overridepublic void onTick(long millisUntilFinished){// First stick calls always returns false. The second stick// always returns true if the countDownInterval is set to 1000.// It is somewhere in between 500 to a 1000.mBluetoothHeadset.startVoiceRecognition(mConnectedHeadset);Log.d(TAGonTick startVoiceRecognition""); //$NON-NLS-1$}@SuppressWarnings(""synthetic-access"")@Overridepublic void onFinish(){// Calls to startVoiceRecognition in onStick are not successful.// Should implement something to inform user of this failuremIsCountDownOn =Log.d(TAGnonFinish fail to connect to headset audio""); //$NON-NLS-1$}};} (日)编辑更改为@TargetApi(Build.VERSION_CODES.HONEYCOMB)必要时。如果你有问题,java.lang.NoClassDefFoundError的API,8或9,只是删除所有的API&=11码。该startBluetoothSco()API适用于所有版本。 + 尽管蓝牙耳机配对并连接到手机音频配置文件(HF / HS),实际的音频连接(SCO)当呼叫才成立和接受。 为你的应用VR从蓝牙耳机接收语音输入应用程序将不得不建立上海合作组织到VR耳机触发输入, 你需要以下- isBluetoothScoAvailableOffCall检查 CodeGo.net,如果平台支持此函数, startBluetoothSco和stopBluetoothSco启动上海合作组织的耳机。 + 我认为,所有你必须改变背面的音频设置为您的应用程序。 如果你推杆以下引脚当你想接收,并通过蓝牙从耳机发出的声音。AudioManager audioMaudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);audioManager.startBluetoothSco();audioManager.setBluetoothScoOn(true);不要忘记恢复以下列方式在手机上正常的设置当你的蓝牙。audioManager.setMode(AudioManager.MODE_NORMAL);audioManager.stopBluetoothSco();audioManager.setBluetoothScoOn(false);有需要的权限如下:这很容易! + startBluetoothSco需要长时间来建立,也就是如果你需要它的语音控制的问题。 有没有快速的方法来蓝牙mic来听录音,然后听完之后把它关掉? 如果连接的是对整个那么它是不可能通过流支持A2DP的音频。 密封式的,理想化的世界: 通过支持A2DP八月音频。当它开始监听,SCO蓝牙mic。任何回应又是支持A2DP。 事实上,如果它已经连接-你可以在飞行中通过改变流切换到流调用? DM跳,有没有明显的延迟? +本文标题 :使用Android RecognizerIntent与蓝牙耳机 +
解决方案三:
接口类型设置了吗?你看看吧
【云栖快讯】直播推荐——现在预约2月28日14:00 VPN网关新品发布会直播,即可赢取SSL-VPN网关一个月免费试用,尽享安全、稳定、快捷的企业级服务!先到先得哦!&&
稳定可靠、可弹性伸缩的在线数据库服务,全球最受欢迎的开源数据库之一
6款热门基础云产品6个月免费体验;2款产品1年体验;1款产品2年体验
弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率
开发者常用软件,超百款实用软件一站式提供

我要回帖

更多关于 蓝牙设备怎么改名字 的文章

 

随机推荐