bluetooth_devices.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. const fileHash = require('../../util/backup_filehash')
  2. const log = require('../../util/log')
  3. const PAIRED_DB = fileHash('Library/Database/com.apple.MobileBluetooth.ledevices.paired.db', 'SysSharedContainerDomain-systemgroup.com.apple.bluetooth')
  4. const OTHER_DB = fileHash('Library/Database/com.apple.MobileBluetooth.ledevices.other.db', 'SysSharedContainerDomain-systemgroup.com.apple.bluetooth')
  5. module.exports = {
  6. version: 4,
  7. name: 'system.bluetooth_devices',
  8. description: `List known bluetooth devices`,
  9. requiresBackup: true,
  10. // Run on a v3 lib / backup object
  11. run (lib, { backup }) {
  12. return getBluetoothDevices(backup)
  13. },
  14. // Available fields.
  15. output: {
  16. uuid: el => el.Uuid || null,
  17. name: el => el.Name ? el.Name : 'N/A',
  18. macAddress: el => {
  19. let address = el.ResolvedAddress ? el.ResolvedAddress : el.Address ? el.Address : 'N/A'
  20. address = address.indexOf(' ') !== -1 ? address.split(' ')[1] : address
  21. return address
  22. },
  23. lastConnected: el => el.LastConnectionTime || 0,
  24. lastSeen: el => el.LastSeenTime || 0,
  25. paired: el => el.Paired ? el.Paired : 'No'
  26. }
  27. }
  28. // Get the bluetooth devices in a backup.
  29. function getBluetoothDevices (backup) {
  30. return new Promise(async (resolve, reject) => {
  31. // Get paired devices
  32. try {
  33. var paired = await getPairedDevices(backup)
  34. } catch (e) {
  35. log.verbose(`couldn't get paired devices`, e)
  36. }
  37. // Get other devices
  38. try {
  39. var other = await getOtherDevices(backup)
  40. } catch (e) {
  41. log.verbose(`couldn't get paired devices`, e)
  42. }
  43. // console.log(paired, other)
  44. resolve([...(paired || []), ...(other || [])])
  45. })
  46. }
  47. // Get devies we've paired with
  48. function getPairedDevices (backup) {
  49. return new Promise((resolve, reject) => {
  50. backup.openDatabase(PAIRED_DB)
  51. .then(paired => {
  52. const query = `SELECT *, 'Yes' as Paired FROM PairedDevices`
  53. paired.all(query, function (err, rows) {
  54. if (err) reject(err)
  55. resolve(rows)
  56. })
  57. })
  58. .catch(reject)
  59. })
  60. }
  61. // Get other devices we've seen.
  62. function getOtherDevices (backup) {
  63. return new Promise((resolve, reject) => {
  64. backup.openDatabase(OTHER_DB)
  65. .then(paired => {
  66. const query = `SELECT * FROM OtherDevices`
  67. paired.all(query, function (err, rows) {
  68. if (err) reject(err)
  69. resolve(rows)
  70. })
  71. })
  72. .catch(reject)
  73. })
  74. }