2012/12/14

Android 腦立方 腦波程式 打電話

慶祝老闆要回火星,特別送上這個開胃菜給他

這個程式必須跟腦立方MindWave藍牙裝置連結,使用前必須先跟MindWave建立通訊頻道,透過這個頻道來撈訊息

進到Resume狀態開始初始化物件,並取得到藍牙橋接器,將藍牙橋接器和高等工人餵給腦力方所提供的Class TGDevice,並且開始監聽訊息

TGDevice有可能是一個高等經紀人型態,所以它才能去撈到通道訊息




撈訊息如果同時是連線成功以及連線中則關閉MindWave通道,接著就可以取得到腦波值,在該範例冥想低於35則會打電話給老闆,哈哈 叫他起床尿尿吧~

TestProject:
/**@(#)TestProject.java    1.0    2012.12.14
 * @author  ChengYou Fang 
 */
package com.cyfang.test;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;

import com.neurosky.thinkgear.TGDevice;

public class TestProject extends Activity {
    private static TGDevice device;
    private static BluetoothAdapter bluetoothAdapter;
    private static boolean State = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_project);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    private final void init() {
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); // 橋接
        if (bluetoothAdapter != null) {
            device = new TGDevice(bluetoothAdapter, handler); // Message Passing
            State = setTGDevice();
        } else {
            Toast.makeText(this, "沒辦法連接到該裝置", Toast.LENGTH_LONG).show();
        }

    }

    private final boolean setTGDevice() {
        if (device.getState() != TGDevice.STATE_CONNECTED
                && device.getState() != TGDevice.STATE_CONNECTING) {
            device.connect(false); // 拒絕其他裝置連線
            return true; // 設定成功
        }
        return false; // 設定失敗
    }

    private final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) { // 取得到Message Passing Signal
            case TGDevice.MSG_STATE_CHANGE:
                switch (msg.arg1) { // 取得通信中的狀態
                case TGDevice.STATE_CONNECTED:
                    device.start();
                    Log.i("Tag", "連線成功");
                    break;
                }
                break;
            case TGDevice.MSG_POOR_SIGNAL:
                Log.i("POOR_SIGNAL:", "" + msg.arg1);
                break;
            case TGDevice.MSG_MEDITATION: // 冥想狀態
                Log.i("MEDITATION:", "" + msg.arg1);
                if (msg.arg1 < 35) { // 如果想睡覺就打給老闆
                    Action.setPhone("921871800", getApplicationContext());
                }
                break;
            case TGDevice.MSG_LOW_BATTERY:
                Toast.makeText(getApplicationContext(), "電池電量過低,將關閉程式",
                        Toast.LENGTH_LONG).show();
                finish(); // 關閉程式
                break;
            }
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        init(); // 初始化
    }

    @Override
    protected void onPause() {
        if (State) {
            State = false;
            device.close(); // 關閉連接
        }
        super.onPause();
    }
}



Action
/**@(#)Action.java    1.0    2012.12.14
 * @author  ChengYou Fang 
 */
package com.cyfang.test;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;

class Action {
    protected static final String phone = "tel:0";
    private static Intent intent = null;

    public static final void setPhone(String str, Context context) {
        intent = new Intent(Intent.ACTION_CALL); // 意圖打電話
        try {
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setData(Uri.parse(phone + Integer.parseInt(str)));// 測試是否為數字
            context.startActivity(intent);
            Log.i("Call", intent.getData().toString());
        } catch (NumberFormatException exception) {
            Toast.makeText(context, "電話號碼有問題", Toast.LENGTH_LONG).show();
        }
    }

}



權限:
uses-permission android:name="android.permission.BLUETOOTH"/>
uses-permission android:name="android.permission.CALL_PHONE"/>