Android以编程方式连接到蓝牙 [英] Android connect to bluetooth programmatically
本文介绍了Android以编程方式连接到蓝牙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我有这个代码以编程方式连接蓝牙,但每当我点击它并打开蓝牙时,应用程序意外停止。这是我的代码。谢谢!
if (mBluetoothAdapter == null){
// 准备警报框
AlertDialog.Builder alertbox = new AlertDialog.Builder( this );
alertbox.setTitle( 蓝牙权限请求);
// 设置要显示的消息
alertbox.setMessage ( 你想打开蓝牙吗?);
// 设置肯定/是按钮并创建一个监听器
alertbox.setPositiveButton( 是, new DialogInterface.OnClickListener(){
// 点击按钮时执行某些操作
public void onClick(DialogInterface arg0, int arg1){
// Toast.makeText(getApplicationContext(),单击是按钮,Toast.LENGTH_SHORT.show();
Intent discoverableIntent = new 意图(BluetoothAdap ter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300 );
startActivity(discoverableIntent);
}
});
解决方案
查看这些..
http://stackoverflow.com/questions/3806536/how-to-enable- disable-bluetooth-programmatically-in-android [ ^ ]
http://stackoverflow.com/questions/6080818/need-to-programmatically-open-bluetooth-and-discover-devices-in-android [ ^
Hi All,
I have this code to connect bluetooth programmatically, but whenever i click it and open the bluetooth the app stopped unexpectedly. This is my code. Thanks!
if (mBluetoothAdapter == null) {
// prepare the alert box
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
alertbox.setTitle("Bluetooth permission request");
// set the message to display
alertbox.setMessage("DO you wish to open your bluetooth?");
// set a positive/yes button and create a listener
alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
//Toast.makeText(getApplicationContext(), "'Yes' button clicked", Toast.LENGTH_SHORT).show();
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
}
});
解决方案
See these..
http://stackoverflow.com/questions/3806536/how-to-enable-disable-bluetooth-programmatically-in-android[^]
http://stackoverflow.com/questions/6080818/need-to-programmatically-open-bluetooth-and-discover-devices-in-android[^]
这篇关于Android以编程方式连接到蓝牙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文