android藍芽編程

來源:互聯網
上載者:User
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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.