android蓝牙编程

package com.ken.testBluetooth;

import android.app.Activity;
import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

public class TestBluetoothActivity extends Activity {
	//获得蓝牙适配器
    BluetoothAdapter mAdapter=BluetoothAdapter.getDefaultAdapter();
	//蓝牙请求打开
	private static final int REQUEST_ENABLE=0x1;
	/* 请求能够被搜索 */
	private static final int REQUEST_DISCOVERABLE = 0x2;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
}
  //打开蓝牙
public void onEnableButtonClicked(View view){
    
    if(!mAdapter.isEnabled()){
    	//弹出对话框提示用户
    	Intent enabler=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    	startActivityForResult(enabler,REQUEST_ENABLE);
    	/**
    	 * 不做提示打开
    	 * mAdapter.enable();
    	 */
    }else{
    	new AlertDialog.Builder(this)
		.setMessage("蓝牙已打开!")
		.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
			}
		})
		.setNegativeButton("取消", null).show();
    }
}
    //关闭蓝牙
    public void onDisableButtonClicked(View view){
    	mAdapter.disable();
    }
    /* 使设备能够被搜索 */
	public void onMakeDiscoverableButtonClicked(View view)
	{

		Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
		startActivityForResult(enabler, REQUEST_DISCOVERABLE);
	}
	/* 开始搜索 */
	public void onStartDiscoveryButtonClicked(View view)
	{

		Intent enabler = new Intent(this, DiscoveryActivity.class);
		startActivity(enabler);
	}
	/* 客户端 */
	public void onOpenClientSocketButtonClicked(View view)
	{

		Intent enabler = new Intent(this, ClientSocketActivity.class);
		startActivity(enabler);
	}
	/* 服务端 */
	public void onOpenServerSocketButtonClicked(View view)
	{

		Intent enabler = new Intent(this, ServerSocketActivity.class);
		startActivity(enabler);
	}
	/* OBEX服务器 */
	public void onOpenOBEXServerSocketButtonClicked(View view)
	{

		Intent enabler = new Intent(this, OBEXActivity.class);
		startActivity(enabler);
	}
}



原文:http://www.eoeandroid.com/thread-18993-1-1.html


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值