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,String,Class,文档,button)