Android 蓝牙开发之一设置蓝牙
有关蓝牙的开发文档在http://developer.android.com/guide/topics/wireless/bluetooth.html
还是看代码,代码简单
package org.piaozhiye; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class BluezDemo1 extends Activity { private BluetoothAdapter bluetooth; private static int DISCOVERY_REQUEST = 1; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); configureBluetooth(); setupListenButton(); } private void configureBluetooth() { // 通过BluetoothAdapter来控制本地蓝牙设备。 bluetooth = BluetoothAdapter.getDefaultAdapter(); String bluetoothInfo; if (bluetooth.isEnabled()) { String address = bluetooth.getAddress(); String name = bluetooth.getName(); bluetoothInfo = name + " :" + address; } else { bluetoothInfo = "Bluetooth is disable!"; } Toast.makeText(this, bluetoothInfo, Toast.LENGTH_LONG).show(); } private void setupListenButton() { Button setting_blue = (Button) findViewById(R.id.blue_setting); setting_blue.setOnClickListener(new OnClickListener() { public void onClick(View view) { Intent disc = new Intent( BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); startActivityForResult(disc, DISCOVERY_REQUEST); } }); } }
同时还需要有权限:
<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
更多相关文章
- Android 代码扫描工具
- android 应用 Ant脚本自动编译、打包、代码混淆、签名、安装等
- Android中处理代码未捕获异常
- Android时间互换代码
- 【Android Demo】获取指定网页的页面源代码
- Android开源项目:捕鱼达人游戏源代码
- 利用productFlavors实现一套代码打包多版本
- android sql 批量处理插入/修改/删除 操作通用代码