import com.theta360.pluginlibrary.values.LedColor
import com.theta360.pluginlibrary.values.LedTarget
import jp.osdn.gokigen.thetathoughtshutter.R.layout
-import jp.osdn.gokigen.thetathoughtshutter.bluetooth.connection.BluetoothDeviceFinder
import jp.osdn.gokigen.thetathoughtshutter.bluetooth.connection.IBluetoothScanResult
import jp.osdn.gokigen.thetathoughtshutter.bluetooth.connection.eeg.MindWaveConnection
import jp.osdn.gokigen.thetathoughtshutter.brainwave.BrainwaveDataHolder
{
private val thetaHardwareControl = ThetaHardwareControl(this)
private val applicationStatus : MyApplicationStatus = MyApplicationStatus()
- //private val bluetoothFinder = BluetoothDeviceFinder(this, this)
- private val bluetoothConnection = MindWaveConnection(this, BrainwaveDataHolder())
+ private val bluetoothConnection = MindWaveConnection(this, BrainwaveDataHolder(), this)
companion object
{
}
if (keyCode == KeyReceiver.KEYCODE_MEDIA_RECORD) // Modeボタン
{
- if (applicationStatus.status == MyApplicationStatus.Status.Searching)
- {
- // ダミー処理 (EEG接続完了)
- applicationStatus.status = MyApplicationStatus.Status.Connected
- }
- else if (applicationStatus.status == MyApplicationStatus.Status.Scanning)
- {
- // ダミー処理 (高まっている状態)
- applicationStatus.status = MyApplicationStatus.Status.Syncing
- }
- else if (applicationStatus.status == MyApplicationStatus.Status.Syncing)
- {
- // ダミー処理 (高まるのを待っている状態)
- applicationStatus.status = MyApplicationStatus.Status.Scanning
- }
- else
- {
- // ダミー処理 (初期化完了)
- applicationStatus.status = MyApplicationStatus.Status.Initialized
+ when (applicationStatus.status) {
+ MyApplicationStatus.Status.Searching -> {
+ // ダミー処理 (EEG接続完了)
+ applicationStatus.status = MyApplicationStatus.Status.Connected
+ }
+ MyApplicationStatus.Status.Scanning -> {
+ // ダミー処理 (高まっている状態)
+ applicationStatus.status = MyApplicationStatus.Status.Syncing
+ }
+ MyApplicationStatus.Status.Syncing -> {
+ // ダミー処理 (高まるのを待っている状態)
+ applicationStatus.status = MyApplicationStatus.Status.Scanning
+ }
+ else -> {
+ // ダミー処理 (初期化完了)
+ applicationStatus.status = MyApplicationStatus.Status.Initialized
+ }
}
}
/*
super.onResume()
if (isApConnected)
{
-
+ Log.v(TAG , " isApConnected : $isApConnected")
}
initializeBluetooth()
}
- override fun onPause()
- {
- super.onPause()
- }
-
private fun initializeBluetooth()
{
try
{
override fun onReceive(context: Context, intent: Intent)
{
- onReceiveBroadcastOfConnection(this, context, intent)
+ onReceiveBroadcastOfConnection(this, intent)
}
}
private fun serialCommunicationMain(btSocket: BluetoothSocket)
{
- Log.v(TAG, "serialCommunicationMain ")
var inputStream: InputStream? = null
try
{
+ Log.v(TAG, "serialCommunicationMain connect")
btSocket.connect()
inputStream = btSocket.inputStream
}
}
if (inputStream == null)
{
+ Log.v(TAG, "serialCommunicationMain INPUT STREAM IS NULL...")
return
}
if (loggingFlag)
e.printStackTrace()
}
}
+ Log.v(TAG, " serialCommunicationMain : SERIAL COMMUNICATION STARTED.")
+
// シリアルデータの受信メイン部分
var previousData = 0xff.toByte()
try
{
val data: Int = inputStream.read()
- Log.v(TAG, " RECEIVED ")
+ //Log.v(TAG, " RECEIVED ")
val byteData = (data and 0xff).toByte()
if (previousData == byteData && byteData == 0xaa.toByte())
{
}
}
-
override fun foundBluetoothDevice(device: BluetoothDevice)
{
try
connectBluetoothDevice(device)
return
}
+ else if (device.bondState == BluetoothDevice.BOND_NONE)
+ {
+ // まだペアリングしていない...
+ Log.v(TAG, " NOT PAIRED, START PAIRING : ${device.name}")
+ isPairing = true
+ targetDevice = device
+ device.setPin(byteArrayOf(0x30, 0x30, 0x30, 0x30))
+ device.createBond()
+ return
+ }
if ((!foundDevice)&&(!isPairing))
{
{
e.printStackTrace()
}
- //return (device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")))
- }
-
-
-/*
- private fun prepareBluetoothDevice(device: BluetoothDevice) : BluetoothSocket?
- {
- try
- {
- device.setPin(byteArrayOf(0x30,0x30, 0x30, 0x30))
-
- val result = device.createBond()
- if (!result)
- {
- // ペアリング失敗
- return (null)
- }
-
-
- }
- catch (e : Exception)
- {
- e.printStackTrace()
- }
- return (device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")))
- }
-
- override fun foundBluetoothDevice(device: BluetoothDevice)
- {
- try
- {
- Log.v(TAG, " foundBluetoothDevice : ${device.name}")
- if (foundDevice)
- {
- // デバイスがすでに見つかっている
- Log.v(TAG, " ALREADY FIND BLUETOOTH DEVICE. : $device.name")
- deviceFinder.stopScan()
- return
- }
- foundDevice = true
- deviceFinder.stopScan()
-
- val btSocket = prepareBluetoothDevice(device) // device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"))
- val thread = Thread {
- try
- {
- if (btSocket != null)
- {
- serialCommunicationMain(btSocket)
- }
- }
- catch (e: Exception)
- {
- e.printStackTrace()
- }
- }
- if (btSocket != null)
- {
- thread.start()
- }
- else
- {
- Log.v(TAG, " btSocket is NULL.")
- }
- scanResult?.foundBluetoothDevice(device)
- }
- catch (e: Exception)
- {
- e.printStackTrace()
- }
}
-*/
private fun connectBluetoothDevice(device : BluetoothDevice?)
{
}
if (btSocket != null)
{
+ scanResult?.foundBluetoothDevice(device)
thread.start()
+ unregisterReceiver()
}
else
{
Log.v(TAG, " btSocket is NULL.")
}
- scanResult?.foundBluetoothDevice(device)
+ //scanResult?.foundBluetoothDevice(device)
}
catch (e: Exception)
{
*
*
*/
- private fun onReceiveBroadcastOfConnection(receiver: BroadcastReceiver, context: Context, intent: Intent)
+ private fun onReceiveBroadcastOfConnection(receiver: BroadcastReceiver, intent: Intent)
{
val action = intent.action
if (action == null)
package jp.osdn.gokigen.thetathoughtshutter.brainwave
import android.util.Log
+import jp.osdn.gokigen.thetathoughtshutter.bluetooth.connection.eeg.MindWaveConnection
import java.util.*
class BrainwaveDataHolder(maxBufferSize: Int = 16000) : IBrainwaveDataReceiver
{
- private val TAG = toString()
+ companion object
+ {
+ private val TAG = BrainwaveDataHolder::class.java.simpleName
+ }
private var valueBuffer: IntArray
private var currentSummaryData = BrainwaveSummaryData()
override fun receivedRawData(value: Int)
{
- Log.v(TAG, " receivedRawData() : $value");
- try {
+ //Log.v(TAG, " receivedRawData() : $value");
+ try
+ {
valueBuffer[currentPosition] = value
currentPosition++
- if (currentPosition == maxBufferSize) {
+ if (currentPosition == maxBufferSize)
+ {
currentPosition = 0
bufferIsFull = true
}
- } catch (e: Exception) {
+ }
+ catch (e: Exception)
+ {
e.printStackTrace()
}
}
{
if (data != null)
{
+ Log.v(TAG, " receivedSummaryData() : ${data.size} bytes.")
if (!currentSummaryData.update(data))
{
// parse failure...