在好例子网,分享、交流、成长!
您当前所在位置:首页Java 开发实例Android平台开发 → android Samsung BLE实例 和蓝牙4.0开发工具包

android Samsung BLE实例 和蓝牙4.0开发工具包

Android平台开发

下载此实例
  • 开发语言:Java
  • 实例大小:0.52M
  • 下载次数:29
  • 浏览次数:927
  • 发布时间:2014-08-26
  • 实例类别:Android平台开发
  • 发 布 人:crazycode
  • 文件格式:.zip
  • 所需积分:2
 相关标签: Android 蓝牙 Samsung

实例介绍

【实例简介】
【实例截图】

【核心代码】

/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.samsung.ble.pxpmonitor;

import android.app.Activity;
import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.res.Configuration;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.util.Log;

public class ProximityMonitorActivity extends Activity implements RadioGroup.OnCheckedChangeListener {
    private static final int REQUEST_SELECT_DEVICE = 1;
    private static final int REQUEST_ENABLE_BT = 2;
    private static final int STATE_READY = 10;
    public static final String TAG = "pxpmonitor";
    private static final int PXP_PROFILE_CONNECTED = 20;
    private static final int PXP_PROFILE_DISCONNECTED = 21;
    private static final int STATE_OFF = 10;

    TextView TxPowerValue, mRemoteRssiVal;
    private int mState = PXP_PROFILE_DISCONNECTED;
    RadioGroup mRg;

    private ProximityService mService = null;
    private BluetoothDevice mDevice = null;
    private BluetoothAdapter mBtAdapter = null;
    private static int mAlertLevel = ProximityService.HIGH_ALERT;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mBtAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBtAdapter == null) {
            Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
            finish();
            return;
        }
        TxPowerValue = ((TextView) findViewById(R.id.statusValue1));
        init();

        ((Button) findViewById(R.id.btn_select)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mBtAdapter.isEnabled()) {
                    Log.i(TAG, "onClick - BT not enabled yet");
                    Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
                }
                else {
                    Intent newIntent = new Intent(ProximityMonitorActivity.this, DeviceListActivity.class);
                    startActivityForResult(newIntent, REQUEST_SELECT_DEVICE);
                }
            }
        });

        ((Button) findViewById(R.id.btn_remove_bond)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mService.removeBond(mDevice);
            }
        });

        ((Button) findViewById(R.id.btn_connect)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mService.connect(mDevice, false);
            }
        });

        ((Button) findViewById(R.id.btn_disconnect)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mService.disconnect(mDevice);
            }
        });

        ((Button) findViewById(R.id.btn_ias_alert)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mService.writeIasAlertLevel(mDevice, mAlertLevel);
            }
        });

        ((Button) findViewById(R.id.btn_lls_alert)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mService.writeLlsAlertLevel(mDevice, mAlertLevel);
            }
        });

        ((Button) findViewById(R.id.btn_write_Tx_Noty)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mService.enableTxPowerNoti(mDevice);
            }
        });

        ((Button) findViewById(R.id.btn_readrssi)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mService.readRssi(mDevice);
            }
        });

        mRemoteRssiVal = (TextView) findViewById(R.id.rssival);

        ((Button) findViewById(R.id.btn_Txpower)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mService.ReadTxPower(mDevice);
            }
        });

        mRg = (RadioGroup) findViewById(R.id.radioGroup1);
        mRg.setOnCheckedChangeListener(this);

        // Set initial UI state
        setUiState();
    }

    private ServiceConnection mServiceConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder rawBinder) {
            mService = ((ProximityService.LocalBinder) rawBinder).getService();
            Log.d(TAG, "onServiceConnected mService= "   mService);
            mService.setActivityHandler(mHandler);
        }

        public void onServiceDisconnected(ComponentName classname) {
            mService.disconnect(mDevice);
            mService = null;
        }
    };

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            final Bundle data = msg.getData();
            switch (msg.what) {
            case ProximityService.PXP_CONNECT_MSG:
                runOnUiThread(new Runnable() {
                    public void run() {
                        if(mDevice!=null && mDevice.getAddress().equals(data.getString(BluetoothDevice.EXTRA_DEVICE))){
                            Log.d(TAG, "PXP_CONNECT_MSG");
                            mState = PXP_PROFILE_CONNECTED;
                            setUiState();
                        }
                    }
                });
                break;
            case ProximityService.PXP_DISCONNECT_MSG:
                runOnUiThread(new Runnable() {
                    public void run() {
                        if(mDevice!=null && mDevice.getAddress().equals(data.getString(BluetoothDevice.EXTRA_DEVICE))){
                            Log.d(TAG, "PXP_DISCONNECT_MSG");
                            mState = PXP_PROFILE_DISCONNECTED;
                            setUiState();
                        }
                    }
                });
                break;

            case ProximityService.PXP_READY_MSG:
                Log.d(TAG, "PXP_READY_MSG");
                runOnUiThread(new Runnable() {
                    public void run() {
                        mState = STATE_READY;
                        setUiState();
                    }
                });
                break;

            case ProximityService.PXP_VALUE_MSG:
                Log.d(TAG, "PXP_VALUE_MSG");
                final byte[] value = data.getByteArray(ProximityService.EXTRA_VALUE);
                runOnUiThread(new Runnable() {
                    public void run() {
                        try {
                            TxPowerValue.setText(""   value[0]);
                        } catch (Exception e) {
                            Log.e(TAG, e.toString());
                        }
                    }
                });
                break;
            case ProximityService.GATT_CHARACTERISTIC_RSSI_MSG:
                Log.d(TAG, "GATT_CHARACTERISTIC_RSSI_MSG");
                final BluetoothDevice RemoteRssidevice = data.getParcelable(ProximityService.EXTRA_DEVICE);
                final int rssi = data.getInt(ProximityService.EXTRA_RSSI);
                Log.d(TAG, "rssi value is"   rssi   " and remote device is"   RemoteRssidevice);
                runOnUiThread(new Runnable() {
                    public void run() {
                        if (mDevice != null && RemoteRssidevice != null && mDevice.equals(RemoteRssidevice)) {
                            if (rssi != 0) {
                                mRemoteRssiVal.setText("Rssi Value ="   String.valueOf(rssi));
                            }
                        }
                    }
                });
                break;
            case ProximityService.PROXIMITY_ALERT_LEVEL_CHANGED_MSG:
                final byte[] value1 = data.getByteArray(ProximityService.EXTRA_VALUE);
                final byte alertLevel = value1[0];
                Log.d(TAG, "PROXIMITY_ALERT_LEVEL_CHANGED_MSG value of alertlevel is "   value1[0]);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (alertLevel != ProximityService.NO_ALERT) {
                            try {
                                Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                                Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
                                r.play();
                            } catch (Exception e) {
                                Log.e(TAG, e.toString());
                            }

                            switch (alertLevel) {
                            case ProximityService.LOW_ALERT:
                                showMessage(getString(R.string.low_alert));
                                break;
                            case ProximityService.HIGH_ALERT:
                                showMessage(getString(R.string.high_alert));
                                break;
                            default:
                                Log.e(TAG,"wrong alert level");
                                break;
                            }
                        }
                    }
                });
                break;
            default:
                super.handleMessage(msg);
            }
        }
    };

    private final BroadcastReceiver proximityStatusChangeReceiver = new BroadcastReceiver() {

        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            final Intent mIntent = intent;
            if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {
                BluetoothDevice device = mIntent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                Log.d(TAG, "BluetoothDevice.ACTION_BOND_STATE_CHANGED");
                if (device.equals(mDevice)) {
                    runOnUiThread(new Runnable() {
                        public void run() {
                            setUiState();
                        }
                    });
                }
            }
            if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
                final int state = mIntent.getIntExtra(BluetoothAdapter.EXTRA_STATE,BluetoothAdapter.ERROR);
                Log.d(TAG, "BluetoothAdapter.ACTION_STATE_CHANGED"   "state is"   state);
                    runOnUiThread(new Runnable() {
                        public void run() {
                            if ( state == STATE_OFF) {
                                mState = PXP_PROFILE_DISCONNECTED;
                                setUiStateForBTOff();
                            }
                        }
                    });
            }
        }
    };

    private void init() {
        Intent bindIntent = new Intent(this, ProximityService.class);
        startService(bindIntent);
        bindService(bindIntent, mServiceConnection, Context.BIND_AUTO_CREATE);

        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        this.registerReceiver(proximityStatusChangeReceiver, filter);
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.d(TAG, "onStart() mService= "   mService);
    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "onDestroy()");
        try {
            unregisterReceiver(proximityStatusChangeReceiver);
        } catch (Exception ignore) {
            Log.e(TAG, ignore.toString());
        }
        unbindService(mServiceConnection);
        stopService(new Intent(this, ProximityService.class));
        super.onDestroy();
    }

    @Override
    protected void onStop() {
        Log.d(TAG, "onStop");
        super.onStop();
    }

    @Override
    protected void onPause() {
        Log.d(TAG, "onPause");
        super.onPause();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "onRestart");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "onResume");
        if (!mBtAdapter.isEnabled()) {
            Log.i(TAG, "onResume - BT not enabled yet");
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
        }
        updateUi();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {

        case REQUEST_SELECT_DEVICE:
            if (resultCode == Activity.RESULT_OK && data != null) {
                String deviceAddress = data.getStringExtra(BluetoothDevice.EXTRA_DEVICE);
                mDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(deviceAddress);
                Log.d(TAG, "... onActivityResultdevice.address=="   mDevice   "mserviceValue"   mService);
                setUiState();
                mService.connect(mDevice, false);
            }
            break;
        case REQUEST_ENABLE_BT:
            // When the request to enable Bluetooth returns
            if (resultCode == Activity.RESULT_OK) {
                Toast.makeText(this, "Bluetooth has turned on ", Toast.LENGTH_SHORT).show();

            } else {
                // User did not enable Bluetooth or an error occurred
                Log.d(TAG, "BT not enabled");
                Toast.makeText(this, "Problem in BT Turning ON ", Toast.LENGTH_SHORT).show();
                finish();
            }
            break;
        default:
            Log.e(TAG, "wrong request code");
            break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == R.id.radio0)
            mAlertLevel = ProximityService.HIGH_ALERT;
        else if (checkedId == R.id.radio1)
            mAlertLevel = ProximityService.LOW_ALERT;
        else if (checkedId == R.id.radio2)
            mAlertLevel = ProximityService.NO_ALERT;
    }

    private void updateUi() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                setUiState();
            }
        });
    }

    private void setUiStateForBTOff() {
        Log.d(TAG, "... setUiStateForBTOff.mState"   mState);
        findViewById(R.id.btn_select).setEnabled(true);
        findViewById(R.id.btn_ias_alert).setEnabled(false);
        findViewById(R.id.btn_lls_alert).setEnabled(false);
        findViewById(R.id.btn_disconnect).setEnabled(false);
        findViewById(R.id.btn_connect).setEnabled(false);
        findViewById(R.id.btn_Txpower).setEnabled(false);
        findViewById(R.id.btn_write_Tx_Noty).setEnabled(false);
        findViewById(R.id.btn_readrssi).setEnabled(false);
        findViewById(R.id.btn_remove_bond).setEnabled(false);

        if (mDevice != null
                && mDevice.getBondState() != BluetoothDevice.BOND_BONDED) {
            Log.i(TAG, "in no device zone");
            ((TextView) findViewById(R.id.deviceName))
                    .setText(R.string.no_device);
        }
        mRemoteRssiVal.setVisibility(View.VISIBLE);
    }

    private void setUiState() {
        findViewById(R.id.btn_select).setEnabled(mState == PXP_PROFILE_DISCONNECTED);
        Log.d(TAG, "... setUiState.mState"   mState);
        findViewById(R.id.btn_ias_alert).setEnabled(mState == STATE_READY);
        findViewById(R.id.btn_lls_alert).setEnabled(mState == STATE_READY);
        findViewById(R.id.btn_disconnect).setEnabled(mState == STATE_READY || mState == PXP_PROFILE_CONNECTED);
        if (mDevice != null) {
            findViewById(R.id.btn_connect).setEnabled(mState == PXP_PROFILE_DISCONNECTED && mDevice.getBondState() == BluetoothDevice.BOND_BONDED);
        }
        else findViewById(R.id.btn_connect).setEnabled(false);
        findViewById(R.id.btn_Txpower).setEnabled(mState == STATE_READY);
        findViewById(R.id.btn_write_Tx_Noty).setEnabled(mState == STATE_READY);
        findViewById(R.id.btn_readrssi).setEnabled(mState == STATE_READY);
        findViewById(R.id.btn_remove_bond).setEnabled(
                mDevice != null && mDevice.getBondState() == BluetoothDevice.BOND_BONDED);

        if (mDevice != null && mDevice.getBondState() != BluetoothDevice.BOND_BONDED) {
            Log.i(TAG, "in no device zone");
            ((TextView) findViewById(R.id.deviceName)).setText(R.string.no_device);
        }
        if (mDevice != null && mDevice.getBondState() == BluetoothDevice.BOND_BONDED) {
            ((TextView) findViewById(R.id.deviceName)).setText(mDevice.getName());
        }
        mRemoteRssiVal.setVisibility(View.VISIBLE);
        TextView status = ((TextView) findViewById(R.id.statusValue));

        switch (mState) {
        case PXP_PROFILE_CONNECTED:
            Log.i(TAG, "STATE_CONNECTED::device name" mDevice.getName());
            status.setText(R.string.connected);
            ((TextView) findViewById(R.id.deviceName)).setText(mDevice.getName());
            break;
        case PXP_PROFILE_DISCONNECTED:
            Log.i(TAG, "disconnected");
            status.setText(R.string.disconnected);
            mRemoteRssiVal.setText(R.string.Noupdate);
            TxPowerValue.setText(R.string.Noupdate);
            break;
        case STATE_READY:
            status.setText(R.string.ready);
            ((TextView) findViewById(R.id.deviceName)).setText(mDevice.getName());
            break;
        default:
            Log.e(TAG, "wrong mState");
            break;
        }
    }

    private void showMessage(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onBackPressed() {
        if (mState == STATE_READY) {
            Intent startMain = new Intent(Intent.ACTION_MAIN);
            startMain.addCategory(Intent.CATEGORY_HOME);
            startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(startMain);
        }
        else {
            new AlertDialog.Builder(this)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setTitle(R.string.popup_title)
            .setMessage(R.string.popup_message)
            .setPositiveButton(R.string.popup_yes, new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            })
            .setNegativeButton(R.string.popup_no, null)
            .show();
        }
    }
}

标签: Android 蓝牙 Samsung

实例下载地址

android Samsung BLE实例 和蓝牙4.0开发工具包

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警