Created
March 26, 2019 16:57
-
-
Save ayyagary/9cd1a1e4d8b384bf8bceb10d8058bfb2 to your computer and use it in GitHub Desktop.
index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var BLE = require('ti.bluetooth'); | |
var myPeripheral; | |
var media = require('permissions/media'); | |
var centralManager = BLE.createCentralManager(); | |
$.btn1.addEventListener('click', function(){ | |
if (centralManager.isScanning()) { | |
alert('Already scanning, please stop scan first!'); | |
return; | |
} else if (centralManager.getState() != BLE.MANAGER_STATE_POWERED_ON) { | |
alert('The BLE manager needs to be powered on before. Call initialize().'); | |
return; | |
} | |
scanForDevices(); | |
}); | |
$.btn2.addEventListener('click', function() { | |
if (!centralManager.isScanning()) { | |
alert('Not scanning!'); | |
return; | |
} | |
centralManager.stopScan(); | |
}); | |
centralManager.addEventListener('didDiscoverPeripheral', function(e) { | |
Ti.API.info('didDiscoverPeripheral'); | |
Ti.API.info(e); | |
Ti.API.info('e.peripheral:', e.peripheral); | |
Ti.API.info('e.peripheral.name:', e.peripheral.name); | |
centralManager.connectPeripheral(e.peripheral, { | |
notifyOnConnection: true, | |
notifyOnDisconnection: true | |
}); | |
centralManager.stopScan(); | |
Ti.API.info('Stopped scanning...........'); | |
}); | |
centralManager.addEventListener('didConnectPeripheral', function(e) { | |
Ti.API.info('Connected to Periperal...'); | |
connectedPeripheral = e.peripheral; | |
connectedPeripheral.addEventListener('didDiscoverServices', function(e) { | |
Ti.API.info('Service Discovery...'); | |
p = e.peripheral; | |
Ti.API.info('peripheral has', p.services.length, 'service(s)'); | |
}); | |
}); | |
$.index.open(); | |
function scanForDevices() { | |
//Check if the user has location service enabled | |
if(!Titanium.Geolocation.hasLocationPermissions(Titanium.Geolocation.AUTHORIZATION_ALWAYS)){ | |
Titanium.Geolocation.requestLocationPermissions(Titanium.Geolocation.AUTHORIZATION_ALWAYS, function(e){ | |
if(!e.success){ | |
alert('Access denied. You must allow this request.','OK'); | |
}else{ | |
centralManager.startScan(); | |
} | |
}); | |
}else{ | |
centralManager.startScan(); | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment